summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-09-28 23:03:39 (GMT)
committerGitHub <noreply@github.com>2022-09-28 23:03:39 (GMT)
commit28f1435d94e72a1fadec2e3d94eac300bb386c2e (patch)
treeaa2bf1439efe09edfaafb8d9f0e514fc373f816b /Lib
parent3d8dfb339b0c0696c5efd4effa9a6b1e34d814e1 (diff)
downloadcpython-28f1435d94e72a1fadec2e3d94eac300bb386c2e.zip
cpython-28f1435d94e72a1fadec2e3d94eac300bb386c2e.tar.gz
cpython-28f1435d94e72a1fadec2e3d94eac300bb386c2e.tar.bz2
gh-97616: list_resize() checks for integer overflow (GH-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*)). (cherry picked from commit a5f092f3c469b674b8d9ccbd4e4377230c9ac7cf) Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_list.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py
index 3c8d829..85bbf51 100644
--- a/Lib/test/test_list.py
+++ b/Lib/test/test_list.py
@@ -68,6 +68,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):