Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 487b73a

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

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
@@ -197,6 +197,12 @@ class T(Array):
197197
_type_ = c_int
198198
_length_ = 0
199199

200+
def test_bpo36504_signed_int_overflow(self):
201+
# The overflow check in PyCArrayType_new() could cause signed integer
202+
# overflow.
203+
with self.assertRaises(OverflowError):
204+
c_char * sys.maxsize * 2
205+
200206
@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
201207
@bigmemtest(size=_2G, memuse=1, dry_run=False)
202208
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
@@ -1518,7 +1518,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15181518
}
15191519

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

0 commit comments

Comments
 (0)