summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-19 10:41:45 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-19 10:41:45 (GMT)
commit9101e23ff6006d9ede7d46e8c9e7d39e23c2a3c3 (patch)
treec61aebdba4dddc8f10c9460ca33b0fa3f2e1495d /Modules
parentbd8f29028eab752e8e5c73703218a701028c1a9a (diff)
parent441d30fac7f4037e4a79e4ada873de3b6f6e5a26 (diff)
downloadcpython-9101e23ff6006d9ede7d46e8c9e7d39e23c2a3c3.zip
cpython-9101e23ff6006d9ede7d46e8c9e7d39e23c2a3c3.tar.gz
cpython-9101e23ff6006d9ede7d46e8c9e7d39e23c2a3c3.tar.bz2
Issue #15989: Fix several occurrences of integer overflow
when result of PyLong_AsLong() narrowed to int without checks. This is a backport of changesets 13e2e44db99d and 525407d89277.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_ctypes/stgdict.c2
-rw-r--r--Modules/_io/fileio.c4
-rw-r--r--Modules/_io/textio.c2
-rw-r--r--Modules/parsermodule.c24
-rw-r--r--Modules/posixmodule.c2
-rw-r--r--Modules/selectmodule.c12
-rw-r--r--Modules/socketmodule.c6
7 files changed, 35 insertions, 17 deletions
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index e5b0e4c..25d9996 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -335,7 +335,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
isPacked = PyObject_GetAttrString(type, "_pack_");
if (isPacked) {
- pack = PyLong_AsLong(isPacked);
+ pack = _PyLong_AsInt(isPacked);
if (pack < 0 || PyErr_Occurred()) {
Py_XDECREF(isPacked);
PyErr_SetString(PyExc_ValueError,
diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c
index ca25209..8c1fabe 100644
--- a/Modules/_io/fileio.c
+++ b/Modules/_io/fileio.c
@@ -244,7 +244,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
return -1;
}
- fd = PyLong_AsLong(nameobj);
+ fd = _PyLong_AsInt(nameobj);
if (fd < 0) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
@@ -382,7 +382,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
goto error;
}
- self->fd = PyLong_AsLong(fdobj);
+ self->fd = _PyLong_AsInt(fdobj);
Py_DECREF(fdobj);
if (self->fd == -1) {
goto error;
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 83437d6..a93049f 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -881,7 +881,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
}
}
else {
- int fd = (int) PyLong_AsLong(fileno);
+ int fd = _PyLong_AsInt(fileno);
Py_DECREF(fileno);
if (fd == -1 && PyErr_Occurred()) {
goto error;
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index fea603e..e86fe4d 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -725,7 +725,7 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
/* elem must always be a sequence, however simple */
PyObject* elem = PySequence_GetItem(tuple, i);
int ok = elem != NULL;
- long type = 0;
+ int type = 0;
char *strn = 0;
if (ok)
@@ -736,8 +736,14 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
ok = 0;
else {
ok = PyLong_Check(temp);
- if (ok)
- type = PyLong_AS_LONG(temp);
+ if (ok) {
+ type = _PyLong_AsInt(temp);
+ if (type == -1 && PyErr_Occurred()) {
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ }
Py_DECREF(temp);
}
}
@@ -773,8 +779,16 @@ build_node_children(PyObject *tuple, node *root, int *line_num)
if (len == 3) {
PyObject *o = PySequence_GetItem(elem, 2);
if (o != NULL) {
- if (PyLong_Check(o))
- *line_num = PyLong_AS_LONG(o);
+ if (PyLong_Check(o)) {
+ int num = _PyLong_AsInt(o);
+ if (num == -1 && PyErr_Occurred()) {
+ Py_DECREF(o);
+ Py_DECREF(temp);
+ Py_DECREF(elem);
+ return 0;
+ }
+ *line_num = num;
+ }
else {
PyErr_Format(parser_error,
"third item in terminal node must be an"
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 25330a0..90838c9 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -8174,7 +8174,7 @@ posix_pipe2(PyObject *self, PyObject *arg)
int fds[2];
int res;
- flags = PyLong_AsLong(arg);
+ flags = _PyLong_AsInt(arg);
if (flags == -1 && PyErr_Occurred())
return NULL;
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 9e53773..ce5ed47 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -357,10 +357,13 @@ update_ufd_array(pollObject *self)
i = pos = 0;
while (PyDict_Next(self->dict, &pos, &key, &value)) {
- self->ufds[i].fd = PyLong_AsLong(key);
+ assert(i < self->ufd_len);
+ /* Never overflow */
+ self->ufds[i].fd = (int)PyLong_AsLong(key);
self->ufds[i].events = (short)PyLong_AsLong(value);
i++;
}
+ assert(i == self->ufd_len);
self->ufd_uptodate = 1;
return 1;
}
@@ -376,10 +379,11 @@ static PyObject *
poll_register(pollObject *self, PyObject *args)
{
PyObject *o, *key, *value;
- int fd, events = POLLIN | POLLPRI | POLLOUT;
+ int fd;
+ short events = POLLIN | POLLPRI | POLLOUT;
int err;
- if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
+ if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) {
return NULL;
}
@@ -518,7 +522,7 @@ poll_poll(pollObject *self, PyObject *args)
tout = PyNumber_Long(tout);
if (!tout)
return NULL;
- timeout = PyLong_AsLong(tout);
+ timeout = _PyLong_AsInt(tout);
Py_DECREF(tout);
if (timeout == -1 && PyErr_Occurred())
return NULL;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index d8c81fe..d7aef8f 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -2073,7 +2073,7 @@ For IP sockets, the address info is a pair (hostaddr, port).");
static PyObject *
sock_setblocking(PySocketSockObject *s, PyObject *arg)
{
- int block;
+ long block;
block = PyLong_AsLong(arg);
if (block == -1 && PyErr_Occurred())
@@ -2555,7 +2555,7 @@ sock_listen(PySocketSockObject *s, PyObject *arg)
int backlog;
int res;
- backlog = PyLong_AsLong(arg);
+ backlog = _PyLong_AsInt(arg);
if (backlog == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS
@@ -3712,7 +3712,7 @@ sock_shutdown(PySocketSockObject *s, PyObject *arg)
int how;
int res;
- how = PyLong_AsLong(arg);
+ how = _PyLong_AsInt(arg);
if (how == -1 && PyErr_Occurred())
return NULL;
Py_BEGIN_ALLOW_THREADS