summaryrefslogtreecommitdiff
path: root/Objects/longobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-19 12:55:39 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-19 12:55:39 +0200
commit74f49ab28b91d3c23524356230feb2724ee9b23f (patch)
tree0ddd5e8899d06c974dfc25a7dfc298e3f6f70039 /Objects/longobject.c
parentac7b49f4076a4336915d13a4aa19feaeadd29d62 (diff)
downloadcpython-git-74f49ab28b91d3c23524356230feb2724ee9b23f.tar.gz
Issue #15989: Fix several occurrences of integer overflow
when result of PyInt_AsLong() or 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.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 5a6338f630..fb740dc4d6 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -339,6 +339,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. */