]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (GH-13881) (#13882)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 7 Jun 2019 08:34:14 +0000 (01:34 -0700)
committerStefan Krah <skrah@bytereef.org>
Fri, 7 Jun 2019 08:34:14 +0000 (10:34 +0200)
Lib/ctypes/test/test_arrays.py
Modules/_ctypes/_ctypes.c

index 37719399e27735e50819b9fc25321d4fcbfa3d6c..9e41f45dc9b1740e14c56b55af926bf9709d2453 100644 (file)
@@ -194,6 +194,21 @@ class ArrayTestCase(unittest.TestCase):
                 _type_ = c_int
                 _length_ = 1.87
 
+    def test_empty_element_struct(self):
+        class EmptyStruct(Structure):
+            _fields_ = []
+
+        obj = (EmptyStruct * 2)()  # bpo37188: Floating point exception
+        assert sizeof(obj) == 0
+
+    def test_empty_element_array(self):
+        class EmptyArray(Array):
+            _type_ = c_int
+            _length_ = 0
+
+        obj = (EmptyArray * 2)()  # bpo37188: Floating point exception
+        assert sizeof(obj) == 0
+
     def test_bpo36504_signed_int_overflow(self):
         # The overflow check in PyCArrayType_new() could cause signed integer
         # overflow.
index 0692b458354c0bf263cf8c615877703579e2b27c..8fa6627552619ec389f0369fd3d4fbf11ebfa803 100644 (file)
@@ -1466,7 +1466,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;