Skip to content

Commit

Permalink
gh-97616: list_resize() checks for integer overflow (GH-97617)
Browse files Browse the repository at this point in the history
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 a5f092f)

Co-authored-by: Victor Stinner <[email protected]>
  • Loading branch information
2 people authored and pablogsal committed Oct 24, 2022
1 parent 9c80b55 commit 38ade0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Lib/test/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ def imul(a, b): a *= b
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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. Patch by Victor Stinner.
10 changes: 8 additions & 2 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ list_resize(PyListObject *self, Py_ssize_t newsize)

if (newsize == 0)
new_allocated = 0;
num_allocated_bytes = new_allocated * sizeof(PyObject *);
items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
if (new_allocated <= (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
num_allocated_bytes = new_allocated * sizeof(PyObject *);
items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
}
else {
// integer overflow
items = NULL;
}
if (items == NULL) {
PyErr_NoMemory();
return -1;
Expand Down

0 comments on commit 38ade0d

Please sign in to comment.