Visitar URL original
bpo-27660: remove unnecessary overflow checks in list_resize by zhangyangyu · Pull Request #189 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix two style nits by Raymond
  • Loading branch information
zhangyangyu committed Feb 21, 2017
commit 529f3333a41a4083fb8894044f61e8dc865bee17
12 changes: 6 additions & 6 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ static int
list_resize(PyListObject *self, Py_ssize_t newsize)
{
PyObject **items;
size_t new_allocated, size;
size_t new_allocated, num_allocated_bytes;
Py_ssize_t allocated = self->allocated;

/* Bypass realloc() when a previous overallocation is large enough
Expand All @@ -45,19 +45,19 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
* sequence of appends() in the presence of a poorly-performing
* system realloc().
* 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);
/* can't overflow, largest number is PY_SSIZE_T_MAX * (9 / 8) + 6 */
new_allocated += (size_t)newsize;
new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6);
if (new_allocated > (size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)) {
PyErr_NoMemory();
return -1;
}

if (newsize == 0)
new_allocated = 0;
size = new_allocated * sizeof(PyObject *);
items = (PyObject **)PyMem_Realloc(self->ob_item, size);
num_allocated_bytes = new_allocated * sizeof(PyObject *);
items = (PyObject **)PyMem_Realloc(self->ob_item, num_allocated_bytes);
if (items == NULL) {
PyErr_NoMemory();
return -1;
Expand Down