Issue #12973: Fix undefined-behaviour-inducing overflow check in list_repeat.

This commit is contained in:
Mark Dickinson 2011-09-19 19:18:37 +01:00
parent bc566b00ad
commit c0420fd42a
2 changed files with 7 additions and 3 deletions

View file

@ -58,7 +58,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
if (newsize == 0)
new_allocated = 0;
items = self->ob_item;
if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
PyMem_RESIZE(items, PyObject *, new_allocated);
else
items = NULL;
@ -510,9 +510,9 @@ list_repeat(PyListObject *a, Py_ssize_t n)
PyObject *elem;
if (n < 0)
n = 0;
size = Py_SIZE(a) * n;
if (n && size/n != Py_SIZE(a))
if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
return PyErr_NoMemory();
size = Py_SIZE(a) * n;
if (size == 0)
return PyList_New(0);
np = (PyListObject *) PyList_New(size);