bpo-26423: Fix possible overflow in wrap_lenfunc() (GH-13606) (GH-13625)

Fix possible overflow in wrap_lenfunc() when
sizeof(long) < sizeof(Py_ssize_t) (e.g., 64-bit Windows).

(cherry picked from commit 05f16416d9)
This commit is contained in:
Victor Stinner 2019-05-28 17:23:07 +02:00 committed by GitHub
parent d9d1045837
commit 80dfe99016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View File

@ -403,6 +403,10 @@ class OperatorsTest(unittest.TestCase):
a.setstate(100)
self.assertEqual(a.getstate(), 100)
def test_wrap_lenfunc_bad_cast(self):
self.assertEqual(xrange(sys.maxsize).__len__(), sys.maxsize)
class ClassPropertiesAndMethods(unittest.TestCase):
def assertHasAttr(self, obj, name):

View File

@ -0,0 +1,2 @@
Fix possible overflow in ``wrap_lenfunc()`` when
``sizeof(long) < sizeof(Py_ssize_t)`` (e.g., 64-bit Windows).

View File

@ -4398,7 +4398,7 @@ wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
res = (*func)(self);
if (res == -1 && PyErr_Occurred())
return NULL;
return PyInt_FromLong((long)res);
return PyInt_FromSsize_t(res);
}
static PyObject *