mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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*)).
This commit is contained in:
parent
81b9d9ddc2
commit
a5f092f3c4
3 changed files with 24 additions and 2 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue