]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36504: Fix signed integer overflow in _ctypes.c's PyCArrayType_new(). (GH-12660)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 3 Apr 2019 17:55:26 +0000 (10:55 -0700)
committerGitHub <noreply@github.com>
Wed, 3 Apr 2019 17:55:26 +0000 (10:55 -0700)
(cherry picked from commit 487b73ab39c80157474821ef9083f51e0846bd62)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Lib/ctypes/test/test_arrays.py
Misc/NEWS.d/next/Core and Builtins/2019-04-02-04-10-32.bpo-36504.k_V8Bm.rst [new file with mode: 0644]
Modules/_ctypes/_ctypes.c

index 6e562cfd24e6638c813188b83be6c408caeeead9..ca271341ed9aa03cc3504ba3d2b02d56b4186bc4 100644 (file)
@@ -183,6 +183,12 @@ class ArrayTestCase(unittest.TestCase):
                 _type_ = c_int
                 _length_ = 1.87
 
+    def test_bpo36504_signed_int_overflow(self):
+        # The overflow check in PyCArrayType_new() could cause signed integer
+        # overflow.
+        with self.assertRaises(OverflowError):
+            c_char * sys.maxsize * 2
+
     @unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
     @bigmemtest(size=_2G, memuse=1, dry_run=False)
     def test_large_array(self, size):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-04-02-04-10-32.bpo-36504.k_V8Bm.rst b/Misc/NEWS.d/next/Core and Builtins/2019-04-02-04-10-32.bpo-36504.k_V8Bm.rst
new file mode 100644 (file)
index 0000000..8ac209d
--- /dev/null
@@ -0,0 +1 @@
+Fix signed integer overflow in _ctypes.c's ``PyCArrayType_new()``.
index be0b321bad035fe9a53c1c6e1b5bc08737a4d2e8..153990309b7acab72d0eea7527773410725c8913 100644 (file)
@@ -1466,7 +1466,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     }
 
     itemsize = itemdict->size;
-    if (length * itemsize < 0) {
+    if (length > PY_SSIZE_T_MAX / itemsize) {
         PyErr_SetString(PyExc_OverflowError,
                         "array too large");
         goto error;