]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44707: Fix an undefined behavior of the null pointer arithmetic (GH-27292) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 29 Jul 2021 12:05:43 +0000 (05:05 -0700)
committerGitHub <noreply@github.com>
Thu, 29 Jul 2021 12:05:43 +0000 (14:05 +0200)
(cherry picked from commit e5c8ddb1714fb51ab1defa24352c98e0f01205dc)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Objects/listobject.c

index f9eb37064a9c975483a4517b158b1ea96db494a7..6c94ba57cfe8eb38c83c717841ce7c9541eb6ebc 100644 (file)
@@ -165,9 +165,10 @@ PyList_New(Py_ssize_t size)
 static PyObject *
 list_new_prealloc(Py_ssize_t size)
 {
+    assert(size > 0);
     PyListObject *op = (PyListObject *) PyList_New(0);
-    if (size == 0 || op == NULL) {
-        return (PyObject *) op;
+    if (op == NULL) {
+        return NULL;
     }
     assert(op->ob_item == NULL);
     op->ob_item = PyMem_New(PyObject *, size);
@@ -446,6 +447,9 @@ list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
     PyObject **src, **dest;
     Py_ssize_t i, len;
     len = ihigh - ilow;
+    if (len <= 0) {
+        return PyList_New(0);
+    }
     np = (PyListObject *) list_new_prealloc(len);
     if (np == NULL)
         return NULL;
@@ -500,6 +504,9 @@ list_concat(PyListObject *a, PyObject *bb)
     if (Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b))
         return PyErr_NoMemory();
     size = Py_SIZE(a) + Py_SIZE(b);
+    if (size == 0) {
+        return PyList_New(0);
+    }
     np = (PyListObject *) list_new_prealloc(size);
     if (np == NULL) {
         return NULL;