]> granicus.if.org Git - python/commitdiff
Issue #18408: Fix list_ass_slice(), handle list_resize() failure
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 19 Jul 2013 21:06:21 +0000 (23:06 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Fri, 19 Jul 2013 21:06:21 +0000 (23:06 +0200)
I tested the patch manually by injecting a fault using gdb: list items are
correctly restored on failure.

Objects/listobject.c

index ce6b70889e8cf2c6813c4f968845c5ed994ecb2d..2f203b343f9243c7b0f8abedf470d47a4a43a947 100644 (file)
@@ -644,9 +644,14 @@ list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
     memcpy(recycle, &item[ilow], s);
 
     if (d < 0) { /* Delete -d items */
-        memmove(&item[ihigh+d], &item[ihigh],
-            (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
-        list_resize(a, Py_SIZE(a) + d);
+        Py_ssize_t tail;
+        tail = (Py_SIZE(a) - ihigh) * sizeof(PyObject *);
+        memmove(&item[ihigh+d], &item[ihigh], tail);
+        if (list_resize(a, Py_SIZE(a) + d) < 0) {
+            memmove(&item[ihigh], &item[ihigh+d], tail);
+            memcpy(&item[ilow], recycle, s);
+            goto Error;
+        }
         item = a->ob_item;
     }
     else if (d > 0) { /* Insert d items */