# HG changeset patch # Parent 9498736fbd8fc06299671aa952a6b9b6b75c089a Issue #1621: Avoid relying on overflow wrapping in ctypes array creation diff -r 9498736fbd8f Lib/ctypes/test/test_arrays.py --- a/Lib/ctypes/test/test_arrays.py Sat Jul 23 07:32:14 2016 +0300 +++ b/Lib/ctypes/test/test_arrays.py Sat Jul 23 05:08:25 2016 +0000 @@ -1,3 +1,4 @@ +import sys import unittest from ctypes import * @@ -161,8 +162,6 @@ self.assertEqual(Y()._length_, 187) def test_bad_subclass(self): - import sys - with self.assertRaises(AttributeError): class T(Array): pass @@ -181,5 +180,10 @@ _type_ = c_int _length_ = 1.87 + def test_size_overflow(self): + half_max = (c_char * (sys.maxsize // 2 + 1)) + with self.assertRaises(OverflowError): + half_max * 2 + if __name__ == '__main__': unittest.main() diff -r 9498736fbd8f Misc/NEWS --- a/Misc/NEWS Sat Jul 23 07:32:14 2016 +0300 +++ b/Misc/NEWS Sat Jul 23 05:08:25 2016 +0000 @@ -38,7 +38,8 @@ - Issue #27567: Expose the EPOLLRDHUP and POLLRDHUP constants in the select module. -- Issue #1621: Avoid signed int negation overflow in the "audioop" module. +- Issue #1621: Avoid signed integer overflow in the "audioop" and "ctypes" + modules. - Issue #27533: Release GIL in nt._isdir diff -r 9498736fbd8f Modules/_ctypes/_ctypes.c --- a/Modules/_ctypes/_ctypes.c Sat Jul 23 07:32:14 2016 +0300 +++ b/Modules/_ctypes/_ctypes.c Sat Jul 23 05:08:25 2016 +0000 @@ -1385,7 +1385,7 @@ sizeof(Py_ssize_t) * (stgdict->ndim - 1)); itemsize = itemdict->size; - if (length * itemsize < 0) { + if (length > PY_SSIZE_T_MAX / itemsize) { PyErr_SetString(PyExc_OverflowError, "array too large"); goto error;