Skip to content

Commit 9c08eeb

Browse files
bpo-36504: Fix signed integer overflow in _ctypes.c's PyCArrayType_new(). (GH-12660)
(cherry picked from commit 487b73a) Co-authored-by: Zackery Spytz <zspytz@gmail.com>
1 parent ef516d1 commit 9c08eeb

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
@@ -183,6 +183,12 @@ class T(Array):
183183
_type_ = c_int
184184
_length_ = 1.87
185185

186+
def test_bpo36504_signed_int_overflow(self):
187+
# The overflow check in PyCArrayType_new() could cause signed integer
188+
# overflow.
189+
with self.assertRaises(OverflowError):
190+
c_char * sys.maxsize * 2
191+
186192
@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
187193
@bigmemtest(size=_2G, memuse=1, dry_run=False)
188194
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
@@ -1466,7 +1466,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14661466
}
14671467

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

0 commit comments

Comments
 (0)