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

Unified Diff: Modules/socketmodule.c

Issue 28332: silent truncations in socket.htons and socket.ntohs
Patch Set: Created 2 years, 11 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_socket.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Modules/socketmodule.c Sat Oct 01 01:06:51 2016 -0500
+++ b/Modules/socketmodule.c Sun Oct 02 09:13:38 2016 +0300
@@ -5498,24 +5498,40 @@
static PyObject *
socket_ntohs(PyObject *self, PyObject *args)
{
- int x1, x2;
+ int x1;
+ long x2;
if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
return NULL;
}
if (x1 < 0) {
PyErr_SetString(PyExc_OverflowError,
- "can't convert negative number to unsigned long");
+ "ntohs: can't convert negative Python int to C "
+ "16-bit unsigned integer");
return NULL;
}
- x2 = (unsigned int)ntohs((unsigned short)x1);
+ if (x1 > 0xffff) {
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "ntohs: Python int too large to convert to C "
+ "16-bit unsigned integer (The silent truncation "
+ "is deprecated)",
+ 1)) {
+ return NULL;
+ }
+ }
+ x2 = (long)ntohs((unsigned short)x1);
return PyLong_FromLong(x2);
}
PyDoc_STRVAR(ntohs_doc,
"ntohs(integer) -> integer\n\
\n\
-Convert a 16-bit integer from network to host byte order.");
+Convert a 16-bit unsigned integer from network to host byte order.\n\
+Note that in case the received integer does not fit in 16-bit unsigned\n\
+integer, but does fit in a positive C int, it is silently truncated to\n\
+16-bit unsigned integer.\n\
+However, this silent truncation feature is deprecated, and will raise an \n\
+exception in future versions of Python.");
static PyObject *
@@ -5555,24 +5571,40 @@
static PyObject *
socket_htons(PyObject *self, PyObject *args)
{
- int x1, x2;
+ int x1;
+ long x2;
if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
return NULL;
}
if (x1 < 0) {
PyErr_SetString(PyExc_OverflowError,
- "can't convert negative number to unsigned long");
+ "htons: can't convert negative Python int to C "
+ "16-bit unsigned integer");
return NULL;
}
- x2 = (unsigned int)htons((unsigned short)x1);
+ if (x1 > 0xffff) {
+ if (PyErr_WarnEx(PyExc_DeprecationWarning,
+ "htons: Python int too large to convert to C "
+ "16-bit unsigned integer (The silent truncation "
+ "is deprecated)",
+ 1)) {
+ return NULL;
+ }
+ }
+ x2 = (long)htons((unsigned short)x1);
return PyLong_FromLong(x2);
}
PyDoc_STRVAR(htons_doc,
"htons(integer) -> integer\n\
\n\
-Convert a 16-bit integer from host to network byte order.");
+Convert a 16-bit unsigned integer from host to network byte order.\n\
+Note that in case the received integer does not fit in 16-bit unsigned\n\
+integer, but does fit in a positive C int, it is silently truncated to\n\
+16-bit unsigned integer.\n\
+However, this silent truncation feature is deprecated, and will raise an \n\
+exception in future versions of Python.");
static PyObject *
« no previous file with comments | « Lib/test/test_socket.py ('k') | no next file » | no next file with comments »

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