summaryrefslogtreecommitdiff
path: root/Lib/test/test_list.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-10-04 10:08:24 -0700
committerGitHub <noreply@github.com>2022-10-04 10:08:24 -0700
commitf9ce9d4684cc293f40171af8fde253e4762baf9d (patch)
treeae03e83b33a40fc44636ae5d4d3a04f60f053470 /Lib/test/test_list.py
parent90620490c04b6894d5a3f4214ecf5a10b41d25ec (diff)
downloadcpython-git-f9ce9d4684cc293f40171af8fde253e4762baf9d.tar.gz
[3.8] gh-97616: list_resize() checks for integer overflow (GH-97617) (GH-97628)
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/test/test_list.py')
-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 105ef650ee..75ecc70b0b 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):