]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-26423: Fix possible overflow in wrap_lenfunc() (GH-13606) (GH-13625)
authorVictor Stinner <vstinner@redhat.com>
Tue, 28 May 2019 15:23:07 +0000 (17:23 +0200)
committerGitHub <noreply@github.com>
Tue, 28 May 2019 15:23:07 +0000 (17:23 +0200)
Fix possible overflow in wrap_lenfunc() when
sizeof(long) < sizeof(Py_ssize_t) (e.g., 64-bit Windows).

(cherry picked from commit 05f16416d99dc9fc76fef11e56f16593e7a5955e)

Lib/test/test_descr.py
Misc/NEWS.d/next/Core and Builtins/2019-05-27-18-00-19.bpo-26423.RgUOE8.rst [new file with mode: 0644]
Objects/typeobject.c

index bbc52aa67913145e06655f9fabf822abf7021005..9b8b8a4b33fae165733b8fe8195eeef57ceb86ca 100644 (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):
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 (file)
index 0000000..6bf2031
--- /dev/null
@@ -0,0 +1,2 @@
+Fix possible overflow in ``wrap_lenfunc()`` when
+``sizeof(long) < sizeof(Py_ssize_t)`` (e.g., 64-bit Windows).
index 56277cfc35ece216d91ba3142a77e2ef835a292b..1c8958c49a3bf4c84bad2341217eb259974bcd91 100644 (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 *