diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-19 10:26:26 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-19 10:26:26 (GMT) |
commit | 441d30fac7f4037e4a79e4ada873de3b6f6e5a26 (patch) | |
tree | a406cb41f1b78476445786f408b95b1cd0bdb7a6 /Objects/longobject.c | |
parent | ff12fae80e15ad29ae2557d23e70f6ff9365b31f (diff) | |
download | cpython-441d30fac7f4037e4a79e4ada873de3b6f6e5a26.zip cpython-441d30fac7f4037e4a79e4ada873de3b6f6e5a26.tar.gz cpython-441d30fac7f4037e4a79e4ada873de3b6f6e5a26.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/longobject.c')
-rw-r--r-- | Objects/longobject.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index 51da329..9ca7b65 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -424,6 +424,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. */ |