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

Skip to content

Commit 8f0bbbd

Browse files
miss-islingtonStefan Krah
authored andcommitted
bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (GH-13881) (#13882)
1 parent 685b806 commit 8f0bbbd

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
@@ -194,6 +194,21 @@ class T(Array):
194194
_type_ = c_int
195195
_length_ = 1.87
196196

197+
def test_empty_element_struct(self):
198+
class EmptyStruct(Structure):
199+
_fields_ = []
200+
201+
obj = (EmptyStruct * 2)() # bpo37188: Floating point exception
202+
assert sizeof(obj) == 0
203+
204+
def test_empty_element_array(self):
205+
class EmptyArray(Array):
206+
_type_ = c_int
207+
_length_ = 0
208+
209+
obj = (EmptyArray * 2)() # bpo37188: Floating point exception
210+
assert sizeof(obj) == 0
211+
197212
def test_bpo36504_signed_int_overflow(self):
198213
# The overflow check in PyCArrayType_new() could cause signed integer
199214
# overflow.

Modules/_ctypes/_ctypes.c

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

14681468
itemsize = itemdict->size;
1469-
if (length > PY_SSIZE_T_MAX / itemsize) {
1469+
if (itemsize != 0 && length > PY_SSIZE_T_MAX / itemsize) {
14701470
PyErr_SetString(PyExc_OverflowError,
14711471
"array too large");
14721472
goto error;

0 commit comments

Comments
 (0)
0