Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6443)

Side by Side Diff: Modules/socketmodule.c

Issue 28332: silent truncations in socket.htons and socket.ntohs
Patch Set: Created 2 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« Lib/test/test_socket.py ('K') | « Lib/test/test_socket.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Socket module */ 1 /* Socket module */
2 2
3 /* 3 /*
4 4
5 This module provides an interface to Berkeley socket IPC. 5 This module provides an interface to Berkeley socket IPC.
6 6
7 Limitations: 7 Limitations:
8 8
9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a 9 - Only AF_INET, AF_INET6 and AF_UNIX address families are supported in a
10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported 10 portable manner, though AF_PACKET, AF_NETLINK and AF_TIPC are supported
(...skipping 5480 matching lines...) Expand 10 before | Expand all | Expand 10 after
5491 socketpair() function.\n\ 5491 socketpair() function.\n\
5492 The arguments are the same as for socket() except the default family is\n\ 5492 The arguments are the same as for socket() except the default family is\n\
5493 AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); 5493 AF_UNIX if defined on the platform; otherwise, the default is AF_INET.");
5494 5494
5495 #endif /* HAVE_SOCKETPAIR */ 5495 #endif /* HAVE_SOCKETPAIR */
5496 5496
5497 5497
5498 static PyObject * 5498 static PyObject *
5499 socket_ntohs(PyObject *self, PyObject *args) 5499 socket_ntohs(PyObject *self, PyObject *args)
5500 { 5500 {
5501 int x1, x2; 5501 int x1;
5502 long x2;
5502 5503
5503 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { 5504 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
5504 return NULL; 5505 return NULL;
5505 } 5506 }
5506 if (x1 < 0) { 5507 if (x1 < 0) {
5507 PyErr_SetString(PyExc_OverflowError, 5508 PyErr_SetString(PyExc_OverflowError,
5508 "can't convert negative number to unsigned long"); 5509 "ntohs: can't convert negative Python int to C "
5510 "16-bit unsigned integer");
5509 return NULL; 5511 return NULL;
5510 } 5512 }
5511 x2 = (unsigned int)ntohs((unsigned short)x1); 5513 if (x1 > USHRT_MAX) {
storchaka 2016/10/01 21:37:42 ntohs() accepts and returns uint16_t. The argument
5514 if (PyErr_WarnEx(PyExc_DeprecationWarning,
5515 "ntohs: Python int too large to convert to C "
5516 "16-bit unsigned integer (The silent truncation "
5517 "is deprecated)",
5518 1)) {
5519 return NULL;
5520 }
5521 }
5522 x2 = (long)ntohs((unsigned short)x1);
storchaka 2016/10/01 21:37:42 What is the reason of changing (unsigned int) to (
Oren Milman 2016/10/02 08:45:24 ntohs returns uint16_t, and x2 is long, so we have
5512 return PyLong_FromLong(x2); 5523 return PyLong_FromLong(x2);
5513 } 5524 }
5514 5525
5515 PyDoc_STRVAR(ntohs_doc, 5526 PyDoc_STRVAR(ntohs_doc,
5516 "ntohs(integer) -> integer\n\ 5527 "ntohs(integer) -> integer\n\
5517 \n\ 5528 \n\
5518 Convert a 16-bit integer from network to host byte order."); 5529 Convert a 16-bit unsigned integer from network to host byte order.\n\
5530 Note that in case the received integer does not fit in 16-bit unsigned\n\
5531 integer, but does fit in a positive C int, it is silently truncated to\n\
5532 16-bit unsigned integer.\n\
5533 However, this silent truncation feature is deprecated, and will raise an \n\
5534 exception in future versions of Python.");
5519 5535
5520 5536
5521 static PyObject * 5537 static PyObject *
5522 socket_ntohl(PyObject *self, PyObject *arg) 5538 socket_ntohl(PyObject *self, PyObject *arg)
5523 { 5539 {
5524 unsigned long x; 5540 unsigned long x;
5525 5541
5526 if (PyLong_Check(arg)) { 5542 if (PyLong_Check(arg)) {
5527 x = PyLong_AsUnsignedLong(arg); 5543 x = PyLong_AsUnsignedLong(arg);
5528 if (x == (unsigned long) -1 && PyErr_Occurred()) 5544 if (x == (unsigned long) -1 && PyErr_Occurred())
(...skipping 19 matching lines...) Expand all
5548 5564
5549 PyDoc_STRVAR(ntohl_doc, 5565 PyDoc_STRVAR(ntohl_doc,
5550 "ntohl(integer) -> integer\n\ 5566 "ntohl(integer) -> integer\n\
5551 \n\ 5567 \n\
5552 Convert a 32-bit integer from network to host byte order."); 5568 Convert a 32-bit integer from network to host byte order.");
5553 5569
5554 5570
5555 static PyObject * 5571 static PyObject *
5556 socket_htons(PyObject *self, PyObject *args) 5572 socket_htons(PyObject *self, PyObject *args)
5557 { 5573 {
5558 int x1, x2; 5574 int x1;
5575 long x2;
5559 5576
5560 if (!PyArg_ParseTuple(args, "i:htons", &x1)) { 5577 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
5561 return NULL; 5578 return NULL;
5562 } 5579 }
5563 if (x1 < 0) { 5580 if (x1 < 0) {
5564 PyErr_SetString(PyExc_OverflowError, 5581 PyErr_SetString(PyExc_OverflowError,
5565 "can't convert negative number to unsigned long"); 5582 "htons: can't convert negative Python int to C "
5583 "16-bit unsigned integer");
5566 return NULL; 5584 return NULL;
5567 } 5585 }
5568 x2 = (unsigned int)htons((unsigned short)x1); 5586 if (x1 > USHRT_MAX) {
5587 if (PyErr_WarnEx(PyExc_DeprecationWarning,
5588 "htons: Python int too large to convert to C "
5589 "16-bit unsigned integer (The silent truncation "
5590 "is deprecated)",
5591 1)) {
5592 return NULL;
5593 }
5594 }
5595 x2 = (long)htons((unsigned short)x1);
5569 return PyLong_FromLong(x2); 5596 return PyLong_FromLong(x2);
5570 } 5597 }
5571 5598
5572 PyDoc_STRVAR(htons_doc, 5599 PyDoc_STRVAR(htons_doc,
5573 "htons(integer) -> integer\n\ 5600 "htons(integer) -> integer\n\
5574 \n\ 5601 \n\
5575 Convert a 16-bit integer from host to network byte order."); 5602 Convert a 16-bit unsigned integer from host to network byte order.\n\
5603 Note that in case the received integer does not fit in 16-bit unsigned\n\
5604 integer, but does fit in a positive C int, it is silently truncated to\n\
5605 16-bit unsigned integer.\n\
5606 However, this silent truncation feature is deprecated, and will raise an \n\
5607 exception in future versions of Python.");
5576 5608
5577 5609
5578 static PyObject * 5610 static PyObject *
5579 socket_htonl(PyObject *self, PyObject *arg) 5611 socket_htonl(PyObject *self, PyObject *arg)
5580 { 5612 {
5581 unsigned long x; 5613 unsigned long x;
5582 5614
5583 if (PyLong_Check(arg)) { 5615 if (PyLong_Check(arg)) {
5584 x = PyLong_AsUnsignedLong(arg); 5616 x = PyLong_AsUnsignedLong(arg);
5585 if (x == (unsigned long) -1 && PyErr_Occurred()) 5617 if (x == (unsigned long) -1 && PyErr_Occurred())
(...skipping 2156 matching lines...) Expand 10 before | Expand all | Expand 10 after
7742 return NULL; 7774 return NULL;
7743 memcpy(&packed_addr, src, sizeof(packed_addr)); 7775 memcpy(&packed_addr, src, sizeof(packed_addr));
7744 return strncpy(dst, inet_ntoa(packed_addr), size); 7776 return strncpy(dst, inet_ntoa(packed_addr), size);
7745 } 7777 }
7746 /* Should set errno to EAFNOSUPPORT */ 7778 /* Should set errno to EAFNOSUPPORT */
7747 return NULL; 7779 return NULL;
7748 } 7780 }
7749 7781
7750 #endif 7782 #endif
7751 #endif 7783 #endif
OLDNEW
« Lib/test/test_socket.py ('K') | « Lib/test/test_socket.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+