10BC0 [2.7] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#1… · python/cpython@48f190f · GitHub
[go: up one dir, main page]

Skip to content

Commit 48f190f

Browse files
eric-wieserStefan Krah
authored andcommitted
[2.7] bpo-37188: Fix a divide-by-zero in arrays of size-0 objects (#13906)
1 parent 2bfc2dc commit 48f190f

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
@@ -134,6 +134,21 @@ class my_int(c_int):
134134
t2 = my_int * 1
135135
self.assertIs(t1, t2)
136136

137+
def test_empty_element_struct(self):
138+
class EmptyStruct(Structure):
139+
_fields_ = []
140+
141+
obj = (EmptyStruct * 2)() # bpo37188: Floating point exception
142+
self.assertEqual(sizeof(obj), 0)
143+
144+
def test_empty_element_array(self):
145+
class EmptyArray(Array):
146+
_type_ = c_int
147+
_length_ = 0
148+
149+
obj = (EmptyArray * 2)() # bpo37188: Floating point exception
150+
self.assertEqual(sizeof(obj), 0)
151+
137152
def test_bpo36504_signed_int_overflow(self):
138153
# The overflow check in PyCArrayType_new() could cause signed integer
139154
# overflow.

Modules/_ctypes/_ctypes.c

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

15361536
itemsize = itemdict->size;
1537-
if (length > PY_SSIZE_T_MAX / itemsize) {
1537+
if (itemsize != 0 && length > PY_SSIZE_T_MAX / itemsize) {
15381538
PyErr_SetString(PyExc_OverflowError,
15391539
"array too large");
15401540
Py_DECREF(stgdict);

0 commit comments

Comments
 (0)
0