]> granicus.if.org Git - python/commitdiff
[3.8] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13911)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 8 Jun 2019 09:46:53 +0000 (02:46 -0700)
committerStefan Krah <skrah@bytereef.org>
Sat, 8 Jun 2019 09:46:53 +0000 (11:46 +0200)
Lib/ctypes/test/test_arrays.py
Modules/_ctypes/_ctypes.c

index 87ecbf04e7edbb6f3ff74f1d7442b01569ebcaa4..14603b7049c92c59032e725c48557a1eaf864a51 100644 (file)
@@ -208,6 +208,21 @@ class ArrayTestCase(unittest.TestCase):
             _type_ = c_int
             _length_ = 0
 
+    def test_empty_element_struct(self):
+        class EmptyStruct(Structure):
+            _fields_ = []
+
+        obj = (EmptyStruct * 2)()  # bpo37188: Floating point exception
+        self.assertEqual(sizeof(obj), 0)
+
+    def test_empty_element_array(self):
+        class EmptyArray(Array):
+            _type_ = c_int
+            _length_ = 0
+
+        obj = (EmptyArray * 2)()  # bpo37188: Floating point exception
+        self.assertEqual(sizeof(obj), 0)
+
     def test_bpo36504_signed_int_overflow(self):
         # The overflow check in PyCArrayType_new() could cause signed integer
         # overflow.
index f7513a3d74c4bc2bf48f65cc01cb03c6f1312e0d..2201c4520ad048f13aec64c7bd2745ea249db23f 100644 (file)
@@ -1518,7 +1518,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     }
 
     itemsize = itemdict->size;
-    if (length > PY_SSIZE_T_MAX / itemsize) {
+    if (itemsize != 0 && length > PY_SSIZE_T_MAX / itemsize) {
         PyErr_SetString(PyExc_OverflowError,
                         "array too large");
         goto error;