Skip to content

Commit c0420fd

Browse files
committed
Issue #12973: Fix undefined-behaviour-inducing overflow check in list_repeat.
1 parent bc566b0 commit c0420fd

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.2.3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #12973: Fix overflow check that relied on undefined behaviour in
14+
list_repeat. This bug caused test_list to fail with recent versions
15+
of Clang.
16+
1317
- Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
1418
mapped to POSIX errno ENOTDIR (previously EINVAL).
1519

Objects/listobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ list_resize(PyListObject *self, Py_ssize_t newsize)
5858
if (newsize == 0)
5959
new_allocated = 0;
6060
items = self->ob_item;
61-
if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
61+
if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
6262
PyMem_RESIZE(items, PyObject *, new_allocated);
6363
else
6464
items = NULL;
@@ -510,9 +510,9 @@ list_repeat(PyListObject *a, Py_ssize_t n)
510510
PyObject *elem;
511511
if (n < 0)
512512
n = 0;
513-
size = Py_SIZE(a) * n;
514-
if (n && size/n != Py_SIZE(a))
513+
if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
515514
return PyErr_NoMemory();
515+
size = Py_SIZE(a) * n;
516516
if (size == 0)
517517
return PyList_New(0);
518518
np = (PyListObject *) PyList_New(size);

0 commit comments

Comments
 (0)