diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-19 10:41:45 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-19 10:41:45 (GMT) |
commit | 9101e23ff6006d9ede7d46e8c9e7d39e23c2a3c3 (patch) | |
tree | c61aebdba4dddc8f10c9460ca33b0fa3f2e1495d /Objects | |
parent | bd8f29028eab752e8e5c73703218a701028c1a9a (diff) | |
parent | 441d30fac7f4037e4a79e4ada873de3b6f6e5a26 (diff) | |
download | cpython-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 'Objects')
-rw-r--r-- | Objects/fileobject.c | 4 | ||||
-rw-r--r-- | Objects/longobject.c | 18 | ||||
-rw-r--r-- | Objects/unicodeobject.c | 4 |
3 files changed, 22 insertions, 4 deletions
diff --git a/Objects/fileobject.c b/Objects/fileobject.c index e1c47ce..3a31314 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -200,7 +200,7 @@ PyObject_AsFileDescriptor(PyObject *o) _Py_IDENTIFIER(fileno); if (PyLong_Check(o)) { - fd = PyLong_AsLong(o); + fd = _PyLong_AsInt(o); } else if ((meth = _PyObject_GetAttrId(o, &PyId_fileno)) != NULL) { @@ -210,7 +210,7 @@ PyObject_AsFileDescriptor(PyObject *o) return -1; if (PyLong_Check(fno)) { - fd = PyLong_AsLong(fno); + fd = _PyLong_AsInt(fno); Py_DECREF(fno); } else { diff --git a/Objects/longobject.c b/Objects/longobject.c index 9535830..32ccdc3 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -434,6 +434,24 @@ PyLong_AsLong(PyObject *obj) return result; } +/* Get a C int from a long int object or any object that has an __int__ + method. Return -1 and set an error if overflow occurs. */ + +int +_PyLong_AsInt(PyObject *obj) +{ + int overflow; + long result = PyLong_AsLongAndOverflow(obj, &overflow); + if (overflow || result > INT_MAX || result < INT_MIN) { + /* XXX: could be cute and give a different + message for overflow == -1 */ + PyErr_SetString(PyExc_OverflowError, + "Python int too large to convert to C int"); + return -1; + } + return (int)result; +} + /* Get a Py_ssize_t from a long int object. Returns -1 and sets an error condition if overflow occurs. */ diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 044b26e..b57a896 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -13569,7 +13569,7 @@ PyUnicode_Format(PyObject *format, PyObject *args) "* wants int"); goto onError; } - width = PyLong_AsLong(v); + width = PyLong_AsSsize_t(v); if (width == -1 && PyErr_Occurred()) goto onError; if (width < 0) { @@ -13609,7 +13609,7 @@ PyUnicode_Format(PyObject *format, PyObject *args) "* wants int"); goto onError; } - prec = PyLong_AsLong(v); + prec = _PyLong_AsInt(v); if (prec == -1 && PyErr_Occurred()) goto onError; if (prec < 0) |