8000 bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13881) · python/cpython@0690c79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0690c79

Browse files
eric-wieserStefan Krah
authored andcommitted
bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13881)
1 parent 554450f commit 0690c79

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+
assert 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+
assert sizeof(obj) == 0
225+
211226
def test_bpo36504_signed_int_overflow(self):
212227
# The overflow che 8BC6 ck 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)
0