diff options
author | Victor Stinner <vstinner@python.org> | 2022-09-28 22:07:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-28 22:07:07 (GMT) |
commit | a5f092f3c469b674b8d9ccbd4e4377230c9ac7cf (patch) | |
tree | 95ce34b8dfa9f92bde8e75260a135e379721d676 /Lib | |
parent | 81b9d9ddc20837ecd19f41b764e3f33d8ae805d5 (diff) | |
download | cpython-a5f092f3c469b674b8d9ccbd4e4377230c9ac7cf.zip cpython-a5f092f3c469b674b8d9ccbd4e4377230c9ac7cf.tar.gz cpython-a5f092f3c469b674b8d9ccbd4e4377230c9ac7cf.tar.bz2 |
gh-97616: list_resize() checks for integer overflow (#97617)
Fix multiplying a list by an integer (list *= int): detect the
integer overflow when the new allocated length is close to the
maximum size. Issue reported by Jordan Limor.
list_resize() now checks for integer overflow before multiplying the
new allocated length by the list item size (sizeof(PyObject*)).
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_list.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index d3da05b..9aa6dd1 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -96,6 +96,19 @@ class ListTest(list_tests.CommonTest): self.assertRaises((MemoryError, OverflowError), mul, lst, n) self.assertRaises((MemoryError, OverflowError), imul, lst, n) + def test_list_resize_overflow(self): + # gh-97616: test new_allocated * sizeof(PyObject*) overflow + # check in list_resize() + lst = [0] * 65 + del lst[1:] + self.assertEqual(len(lst), 1) + + size = ((2 ** (tuple.__itemsize__ * 8) - 1) // 2) + with self.assertRaises((MemoryError, OverflowError)): + lst * size + with self.assertRaises((MemoryError, OverflowError)): + lst *= size + def test_repr_large(self): # Check the repr of large list objects def check(n): |