summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-01-14 17:03:32 (GMT)
committerGuido van Rossum <guido@python.org>2007-01-14 17:03:32 (GMT)
commitbb2cc698c1dd69ad57450bb48d64f2b8aaae7bb7 (patch)
tree7a5adf061a1281e0bc2add6ac069a779274a6d26 /Modules
parent8ef1cf30b7a3427c42fe803547c6f7f33187783f (diff)
downloadcpython-bb2cc698c1dd69ad57450bb48d64f2b8aaae7bb7.zip
cpython-bb2cc698c1dd69ad57450bb48d64f2b8aaae7bb7.tar.gz
cpython-bb2cc698c1dd69ad57450bb48d64f2b8aaae7bb7.tar.bz2
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')
-rw-r--r--Modules/socketmodule.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 8ec0ed7..82461d4 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);
}
@@ -3487,6 +3492,11 @@ socket_ntohl(PyObject *self, PyObject *arg)
x = PyInt_AS_LONG(arg);
if (x == (unsigned long) -1 && PyErr_Occurred())
return NULL;
+ if ((long)x < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
}
else if (PyLong_Check(arg)) {
x = PyLong_AsUnsignedLong(arg);
@@ -3510,7 +3520,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,
@@ -3527,7 +3537,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);
}
@@ -3546,6 +3561,11 @@ socket_htonl(PyObject *self, PyObject *arg)
x = PyInt_AS_LONG(arg);
if (x == (unsigned long) -1 && PyErr_Occurred())
return NULL;
+ if ((long)x < 0) {
+ PyErr_SetString(PyExc_OverflowError,
+ "can't convert negative number to unsigned long");
+ return NULL;
+ }
}
else if (PyLong_Check(arg)) {
x = PyLong_AsUnsignedLong(arg);
@@ -3567,7 +3587,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,