Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/ctypes/test/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ class T(Array):
_type_ = c_int
_length_ = 0

def test_bpo36504_signed_int_overflow(self):
# The overflow check in PyCArrayType_new() could cause signed integer
# overflow.
with self.assertRaises(OverflowError):
c_char * sys.maxsize * 2

@unittest.skipUnless(sys.maxsize > 2**32, 'requires 64bit platform')
@bigmemtest(size=_2G, memuse=1, dry_run=False)
def test_large_array(self, size):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix signed integer overflow in _ctypes.c's ``PyCArrayType_new()``.
2 changes: 1 addition & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

itemsize = itemdict->size;
if (length * itemsize < 0) {
if (length > PY_SSIZE_T_MAX / itemsize) {

@eric-wieser eric-wieser Jun 7, 2019

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Divide-by-zero for:

class S(ctypes.Structure): _fields_ = []

(S*10)()

bpo13881

PyErr_SetString(PyExc_OverflowError,
"array too large");
goto error;
Expand Down