summaryrefslogtreecommitdiffstats
path: root/Modules/socketmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-01-15 00:07:32 (GMT)
committerGuido van Rossum <guido@python.org>2007-01-15 00:07:32 (GMT)
commit018919aba81093e43d5c5c401a253b0707a8e86f (patch)
treebe6e200e2ee5962ab4d57449ab66dec01442eef7 /Modules/socketmodule.c
parent65eabe30e4f3841a32383d9e3dda76573c2b8934 (diff)
downloadcpython-018919aba81093e43d5c5c401a253b0707a8e86f.zip
cpython-018919aba81093e43d5c5c401a253b0707a8e86f.tar.gz
cpython-018919aba81093e43d5c5c401a253b0707a8e86f.tar.bz2
Merged revisions 53434 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r53434 | guido.van.rossum | 2007-01-14 09:03:32 -0800 (Sun, 14 Jan 2007) | 3 lines Patch #1635058 by Mark Roberts: ensure that htonl and friends never accept or return negative numbers, per the underlying C implementation. ........
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r--Modules/socketmodule.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 31efa0a..0f7c9ad 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3468,7 +3468,12 @@ socket_ntohs(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:ntohs", &x1)) {
return NULL;
}
- x2 = (int)ntohs((short)x1);
+ if (x1 < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
+ x2 = (unsigned int)ntohs((unsigned short)x1);
return PyInt_FromLong(x2);
}
@@ -3505,7 +3510,7 @@ socket_ntohl(PyObject *self, PyObject *arg)
arg->ob_type->tp_name);
if (x == (unsigned long) -1 && PyErr_Occurred())
return NULL;
- return PyInt_FromLong(ntohl(x));
+ return PyLong_FromUnsignedLong(ntohl(x));
}
PyDoc_STRVAR(ntohl_doc,
@@ -3522,7 +3527,12 @@ socket_htons(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:htons", &x1)) {
return NULL;
}
- x2 = (int)htons((short)x1);
+ if (x1 < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
+ x2 = (unsigned int)htons((unsigned short)x1);
return PyInt_FromLong(x2);
}
@@ -3557,7 +3567,7 @@ socket_htonl(PyObject *self, PyObject *arg)
return PyErr_Format(PyExc_TypeError,
"expected int/long, %s found",
arg->ob_type->tp_name);
- return PyInt_FromLong(htonl(x));
+ return PyLong_FromUnsignedLong(htonl((unsigned long)x));
}
PyDoc_STRVAR(htonl_doc,