diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-10-02 09:34:40 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-10-02 09:34:40 (GMT) |
commit | 6a7d3480fc8fe4c60538547d2935083ccff0f631 (patch) | |
tree | 9a6a13be60de50579071c31c48079b980aee0bd1 /Modules/socketmodule.c | |
parent | a5749197aaecefef522c13544471a9cb571ab5b6 (diff) | |
download | cpython-6a7d3480fc8fe4c60538547d2935083ccff0f631.zip cpython-6a7d3480fc8fe4c60538547d2935083ccff0f631.tar.gz cpython-6a7d3480fc8fe4c60538547d2935083ccff0f631.tar.bz2 |
Issue #28332: Deprecated silent truncations in socket.htons and socket.ntohs.
Original patch by Oren Milman.
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r-- | Modules/socketmodule.c | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index d25bd7f..9709186 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -5498,24 +5498,38 @@ AF_UNIX if defined on the platform; otherwise, the default is AF_INET."); static PyObject * socket_ntohs(PyObject *self, PyObject *args) { - int x1, x2; + int x; - if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) { + if (!PyArg_ParseTuple(args, "i:ntohs", &x)) { return NULL; } - if (x1 < 0) { + if (x < 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); - return PyLong_FromLong(x2); + if (x > 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; + } + } + return PyLong_FromUnsignedLong(ntohs((unsigned short)x)); } 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 +5569,38 @@ Convert a 32-bit integer from network to host byte order."); static PyObject * socket_htons(PyObject *self, PyObject *args) { - int x1, x2; + int x; - if (!PyArg_ParseTuple(args, "i:htons", &x1)) { + if (!PyArg_ParseTuple(args, "i:htons", &x)) { return NULL; } - if (x1 < 0) { + if (x < 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); - return PyLong_FromLong(x2); + if (x > 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; + } + } + return PyLong_FromUnsignedLong(htons((unsigned short)x)); } 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 * |