Skip to content

Commit

Permalink
Issue python#12973: Fix undefined-behaviour-inducing overflow check i…
Browse files Browse the repository at this point in the history
…n list_repeat.
  • Loading branch information
mdickinson committed Sep 19, 2011
1 parent bc566b0 commit c0420fd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ What's New in Python 3.2.3?
Core and Builtins
-----------------

- Issue #12973: Fix overflow check that relied on undefined behaviour in
list_repeat. This bug caused test_list to fail with recent versions
of Clang.

- Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
mapped to POSIX errno ENOTDIR (previously EINVAL).

Expand Down
6 changes: 3 additions & 3 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c0420fd

Please sign in to comment.