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).
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index e37a984..6b018cc 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -389,6 +389,10 @@
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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-27-18-00-19.bpo-26423.RgUOE8.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-27-18-00-19.bpo-26423.RgUOE8.rst
new file mode 100644
index 0000000..6bf2031
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-05-27-18-00-19.bpo-26423.RgUOE8.rst
@@ -0,0 +1,2 @@
+Fix possible overflow in ``wrap_lenfunc()`` when
+``sizeof(long) < sizeof(Py_ssize_t)`` (e.g., 64-bit Windows).
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 06e045b..c14cbad 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -5536,7 +5536,7 @@
res = (*func)(self);
if (res == -1 && PyErr_Occurred())
return NULL;
- return PyLong_FromLong((long)res);
+ return PyLong_FromSsize_t(res);
}
static PyObject *