[Python-checkins] [3.8] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13911)

Stefan Krah webhook-mailer at python.org
Sat Jun 8 05:46:59 EDT 2019


https://github.com/python/cpython/commit/3d03a35ba0f162a350898100efc95fdf392070a2
commit: 3d03a35ba0f162a350898100efc95fdf392070a2
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Stefan Krah <skrah at bytereef.org>
date: 2019-06-08T11:46:53+02:00
summary:

[3.8] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13911)

files:
M Lib/ctypes/test/test_arrays.py
M Modules/_ctypes/_ctypes.c

diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py
index 87ecbf04e7ed..14603b7049c9 100644
--- a/Lib/ctypes/test/test_arrays.py
+++ b/Lib/ctypes/test/test_arrays.py
@@ -208,6 +208,21 @@ class T(Array):
             _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.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index f7513a3d74c4..2201c4520ad0 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -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;



More information about the Python-checkins mailing list