Skip to content

Commit fd83a82

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-36504: Fix signed integer overflow in _ctypes.c's PyCArrayType_new(). (GH-12660) (GH-12678)
(cherry picked from commit 487b73a)
1 parent a110817 commit fd83a82

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lib/ctypes/test/test_arrays.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ class my_int(c_int):
134134
t2 = my_int * 1
135135
self.assertIs(t1, t2)
136136

137+
def test_bpo36504_signed_int_overflow(self):
138+
# The overflow check in PyCArrayType_new() could cause signed integer
139+
# overflow.
140+
with self.assertRaises(OverflowError):
141+
c_char * sys.maxsize * 2
142+
137143
@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
138144
@precisionbigmemtest(size=_2G, memuse=1, dry_run=False)
139145
def test_large_array(self, size):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix signed integer overflow in _ctypes.c's ``PyCArrayType_new()``.

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 * itemsize < 0) {
1537+
if (length > PY_SSIZE_T_MAX / itemsize) {
15381538
PyErr_SetString(PyExc_OverflowError,
15391539
"array too large");
15401540
Py_DECREF(stgdict);

0 commit comments

Comments
 (0)