From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 7 Jun 2019 08:34:14 +0000 (-0700) Subject: bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (GH-13881) (#13882) X-Git-Tag: v3.7.4rc1~50 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8f0bbbdcae73f275faff90cc4559f6111116f001;p=thirdparty%2FPython%2Fcpython.git bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (GH-13881) (#13882) --- diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py index 37719399e277..9e41f45dc9b1 100644 --- a/Lib/ctypes/test/test_arrays.py +++ b/Lib/ctypes/test/test_arrays.py @@ -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. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 0692b458354c..8fa662755261 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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;