Skip to content

Commit 3d03a35

Browse files
miss-islingtonStefan Krah
authored andcommitted
[3.8] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13911)
1 parent 3b5bac2 commit 3d03a35

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/ctypes/test/test_arrays.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ class T(Array):
208208
_type_ = c_int
209209
_length_ = 0
210210

211+
def test_empty_element_struct(self):
212+
class EmptyStruct(Structure):
213+
_fields_ = []
214+
215+
obj = (EmptyStruct * 2)() # bpo37188: Floating point exception
216+
self.assertEqual(sizeof(obj), 0)
217+
218+
def test_empty_element_array(self):
219+
class EmptyArray(Array):
220+
_type_ = c_int
221+
_length_ = 0
222+
223+
obj = (EmptyArray * 2)() # bpo37188: Floating point exception
224+
self.assertEqual(sizeof(obj), 0)
225+
211226
def test_bpo36504_signed_int_overflow(self):
212227
# The overflow check in PyCArrayType_new() could cause signed integer
213228
# overflow.

Modules/_ctypes/_ctypes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15181518
}
15191519

15201520
itemsize = itemdict->size;
1521-
if (length > PY_SSIZE_T_MAX / itemsize) {
1521+
if (itemsize != 0 && length > PY_SSIZE_T_MAX / itemsize) {
15221522
PyErr_SetString(PyExc_OverflowError,
15231523
"array too large");
15241524
goto error;

0 commit comments

Comments
 (0)