Skip to content

Commit 12fc0d2

Browse files
bpo-44707: Fix an undefined behavior of the null pointer arithmetic (GH-27292) (GH-27443)
(cherry picked from commit e5c8ddb) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 7922546 commit 12fc0d2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Objects/listobject.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ PyList_New(Py_ssize_t size)
165165
static PyObject *
166166
list_new_prealloc(Py_ssize_t size)
167167
{
168+
assert(size > 0);
168169
PyListObject *op = (PyListObject *) PyList_New(0);
169-
if (size == 0 || op == NULL) {
170-
return (PyObject *) op;
170+
if (op == NULL) {
171+
return NULL;
171172
}
172173
assert(op->ob_item == NULL);
173174
op->ob_item = PyMem_New(PyObject *, size);
@@ -446,6 +447,9 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
446447
PyObject **src, **dest;
447448
Py_ssize_t i, len;
448449
len = ihigh - ilow;
450+
if (len <= 0) {
451+
return PyList_New(0);
452+
}
449453
np = (PyListObject *) list_new_prealloc(len);
450454
if (np == NULL)
451455
return NULL;
@@ -500,6 +504,9 @@ list_concat(PyListObject *a, PyObject *bb)
500504
if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
501505
return PyErr_NoMemory();
502506
size = Py_SIZE(a) + Py_SIZE(b);
507+
if (size == 0) {
508+
return PyList_New(0);
509+
}
503510
np = (PyListObject *) list_new_prealloc(size);
504511
if (np == NULL) {
505512
return NULL;

0 commit comments

Comments
 (0)