]> granicus.if.org Git - python/commitdiff
bpo-36504: Fix signed integer overflow in _ctypes.c's PyCArrayType_new(). (GH-12660)
authorZackery Spytz <zspytz@gmail.com>
Tue, 2 Apr 2019 10:47:51 +0000 (04:47 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 2 Apr 2019 10:47:51 +0000 (13:47 +0300)
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 6cfda8b7d2e630e4b3e856508febc7578dc84f44..0fc5d7ebf84106b861a42dc81db8ee8b0370674a 100644 (file)
@@ -197,6 +197,12 @@ class ArrayTestCase(unittest.TestCase):
             _type_ = c_int
             _length_ = 0
 
+    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 b3a20309472de64b96a172b8dd79c08097993641..ac071bbb708b152f38b786ca4ad7799163b7c729 100644 (file)
@@ -1518,7 +1518,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;