mirror of
https://github.com/python/cpython.git
synced 2025-08-01 15:43:13 +00:00
bpo-27660: remove unnecessary overflow checks in list_resize (GH-189)
This commit is contained in:
parent
3e8d6cb189
commit
4cee049f5b
1 changed files with 7 additions and 12 deletions
|
@ -26,7 +26,7 @@ static int
|
||||||
list_resize(PyListObject *self, Py_ssize_t newsize)
|
list_resize(PyListObject *self, Py_ssize_t newsize)
|
||||||
{
|
{
|
||||||
PyObject **items;
|
PyObject **items;
|
||||||
size_t new_allocated;
|
size_t new_allocated, num_allocated_bytes;
|
||||||
Py_ssize_t allocated = self->allocated;
|
Py_ssize_t allocated = self->allocated;
|
||||||
|
|
||||||
/* Bypass realloc() when a previous overallocation is large enough
|
/* Bypass realloc() when a previous overallocation is large enough
|
||||||
|
@ -45,24 +45,19 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
|
||||||
* sequence of appends() in the presence of a poorly-performing
|
* sequence of appends() in the presence of a poorly-performing
|
||||||
* system realloc().
|
* system realloc().
|
||||||
* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
|
* The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
|
||||||
|
* Note: new_allocated won't overflow because the largest possible value
|
||||||
|
* is PY_SSIZE_T_MAX * (9 / 8) + 6 which always fits in a size_t.
|
||||||
*/
|
*/
|
||||||
new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
|
new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
|
||||||
|
if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
|
||||||
/* check for integer overflow */
|
|
||||||
if (new_allocated > SIZE_MAX - newsize) {
|
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
|
||||||
new_allocated += newsize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newsize == 0)
|
if (newsize == 0)
|
||||||
new_allocated = 0;
|
new_allocated = 0;
|
||||||
items = self->ob_item;
|
num_allocated_bytes = new_allocated * sizeof(PyObject *);
|
||||||
if (new_allocated <= (SIZE_MAX / sizeof(PyObject *)))
|
items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
|
||||||
PyMem_RESIZE(items, PyObject *, new_allocated);
|
|
||||||
else
|
|
||||||
items = NULL;
|
|
||||||
if (items == NULL) {
|
if (items == NULL) {
|
||||||
PyErr_NoMemory();
|
PyErr_NoMemory();
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue