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

Delta Between Two Patch Sets: Modules/socketmodule.c

Issue 28332: silent truncations in socket.htons and socket.ntohs
Left Patch Set: Created 2 years, 7 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_socket.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 5492 matching lines...) Expand 10 before | Expand all | Expand 10 after
5503 5503
5504 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { 5504 if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
5505 return NULL; 5505 return NULL;
5506 } 5506 }
5507 if (x1 < 0) { 5507 if (x1 < 0) {
5508 PyErr_SetString(PyExc_OverflowError, 5508 PyErr_SetString(PyExc_OverflowError,
5509 "ntohs: can't convert negative Python int to C " 5509 "ntohs: can't convert negative Python int to C "
5510 "16-bit unsigned integer"); 5510 "16-bit unsigned integer");
5511 return NULL; 5511 return NULL;
5512 } 5512 }
5513 if (x1 > USHRT_MAX) { 5513 if (x1 > 0xffff) {
storchaka 2016/10/01 21:37:42 ntohs() accepts and returns uint16_t. The argument
5514 if (PyErr_WarnEx(PyExc_DeprecationWarning, 5514 if (PyErr_WarnEx(PyExc_DeprecationWarning,
5515 "ntohs: Python int too large to convert to C " 5515 "ntohs: Python int too large to convert to C "
5516 "16-bit unsigned integer (The silent truncation " 5516 "16-bit unsigned integer (The silent truncation "
5517 "is deprecated)", 5517 "is deprecated)",
5518 1)) { 5518 1)) {
5519 return NULL; 5519 return NULL;
5520 } 5520 }
5521 } 5521 }
5522 x2 = (long)ntohs((unsigned short)x1); 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
5523 return PyLong_FromLong(x2); 5523 return PyLong_FromLong(x2);
5524 } 5524 }
5525 5525
5526 PyDoc_STRVAR(ntohs_doc, 5526 PyDoc_STRVAR(ntohs_doc,
5527 "ntohs(integer) -> integer\n\ 5527 "ntohs(integer) -> integer\n\
5528 \n\ 5528 \n\
5529 Convert a 16-bit unsigned integer from network to host byte order.\n\ 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\ 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\ 5531 integer, but does fit in a positive C int, it is silently truncated to\n\
5532 16-bit unsigned integer.\n\ 5532 16-bit unsigned integer.\n\
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
5576 5576
5577 if (!PyArg_ParseTuple(args, "i:htons", &x1)) { 5577 if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
5578 return NULL; 5578 return NULL;
5579 } 5579 }
5580 if (x1 < 0) { 5580 if (x1 < 0) {
5581 PyErr_SetString(PyExc_OverflowError, 5581 PyErr_SetString(PyExc_OverflowError,
5582 "htons: can't convert negative Python int to C " 5582 "htons: can't convert negative Python int to C "
5583 "16-bit unsigned integer"); 5583 "16-bit unsigned integer");
5584 return NULL; 5584 return NULL;
5585 } 5585 }
5586 if (x1 > USHRT_MAX) { 5586 if (x1 > 0xffff) {
5587 if (PyErr_WarnEx(PyExc_DeprecationWarning, 5587 if (PyErr_WarnEx(PyExc_DeprecationWarning,
5588 "htons: Python int too large to convert to C " 5588 "htons: Python int too large to convert to C "
5589 "16-bit unsigned integer (The silent truncation " 5589 "16-bit unsigned integer (The silent truncation "
5590 "is deprecated)", 5590 "is deprecated)",
5591 1)) { 5591 1)) {
5592 return NULL; 5592 return NULL;
5593 } 5593 }
5594 } 5594 }
5595 x2 = (long)htons((unsigned short)x1); 5595 x2 = (long)htons((unsigned short)x1);
5596 return PyLong_FromLong(x2); 5596 return PyLong_FromLong(x2);
(...skipping 2177 matching lines...) Expand 10 before | Expand all | Expand 10 after
7774 return NULL; 7774 return NULL;
7775 memcpy(&packed_addr, src, sizeof(packed_addr)); 7775 memcpy(&packed_addr, src, sizeof(packed_addr));
7776 return strncpy(dst, inet_ntoa(packed_addr), size); 7776 return strncpy(dst, inet_ntoa(packed_addr), size);
7777 } 7777 }
7778 /* Should set errno to EAFNOSUPPORT */ 7778 /* Should set errno to EAFNOSUPPORT */
7779 return NULL; 7779 return NULL;
7780 } 7780 }
7781 7781
7782 #endif 7782 #endif
7783 #endif 7783 #endif
LEFTRIGHT

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