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

Fix possible overflow in wrap_lenfunc() when
sizeof(long) < sizeof(Py_ssize_t) (e.g., 64-bit Windows).
(cherry picked from commit 05f16416d9)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
Miss Islington (bot) 2019-05-28 06:17:43 -07:00 committed by GitHub
parent 1cfb90b69f
commit e7ddf586ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View file

@ -384,6 +384,10 @@ def foo(self): return 1
a.setstate(100)
self.assertEqual(a.getstate(), 100)
def test_wrap_lenfunc_bad_cast(self):
self.assertEqual(range(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

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