]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 19 Dec 2018 15:11:02 +0000 (17:11 +0200)
committerGitHub <noreply@github.com>
Wed, 19 Dec 2018 15:11:02 +0000 (17:11 +0200)
In _localemodule.c and selectmodule.c, remove dead code that would
cause double decrefs if run.

In addition, replace PyList_SetItem() with PyList_SET_ITEM() in cases
where a new list is populated and there is no possibility of an error.

In addition, check if the list changed size in the loop in array_array_fromlist().
(cherry picked from commit 99d56b53560b3867844472ae381fb3f858760621)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
Modules/_localemodule.c
Modules/arraymodule.c
Modules/readline.c
Modules/selectmodule.c
Python/ceval.c
Python/sysmodule.c

index 7e2f1a997be68fcb5489e3381b4869ceb0b6c1dc..c55bd0d846eb84268fe32c7c3e8fc49c8592cca3 100644 (file)
@@ -73,20 +73,13 @@ copy_grouping(char* s)
     do {
         i++;
         val = PyInt_FromLong(s[i]);
-        if (!val)
-            break;
-        if (PyList_SetItem(result, i, val)) {
-            Py_DECREF(val);
-            val = NULL;
-            break;
+        if (val == NULL) {
+            Py_DECREF(result);
+            return NULL;
         }
+        PyList_SET_ITEM(result, i, val);
     } while (s[i] != '\0' && s[i] != CHAR_MAX);
 
-    if (!val) {
-        Py_DECREF(result);
-        return NULL;
-    }
-
     return result;
 }
 
index 5bd3a42f009d1f30f5a325b659ab6a8f526e12a5..8d4eb0932df5770c41a0fbb6a7fbd5d1c5a586f9 100644 (file)
@@ -1344,7 +1344,7 @@ array_fromlist(arrayobject *self, PyObject *list)
         Py_SIZE(self) += n;
         self->allocated = Py_SIZE(self);
         for (i = 0; i < n; i++) {
-            PyObject *v = PyList_GetItem(list, i);
+            PyObject *v = PyList_GET_ITEM(list, i);
             if ((*self->ob_descr->setitem)(self,
                             Py_SIZE(self) - n + i, v) != 0) {
                 Py_SIZE(self) -= n;
@@ -1357,6 +1357,19 @@ array_fromlist(arrayobject *self, PyObject *list)
                 self->allocated = Py_SIZE(self);
                 return NULL;
             }
+            if (n != PyList_GET_SIZE(list)) {
+                PyErr_SetString(PyExc_RuntimeError,
+                                "list changed size during iteration");
+                Py_SIZE(self) -= n;
+                if (itemsize && (Py_SIZE(self) > PY_SSIZE_T_MAX / itemsize)) {
+                    return PyErr_NoMemory();
+                }
+                PyMem_RESIZE(item, char,
+                                  Py_SIZE(self) * itemsize);
+                self->ob_item = item;
+                self->allocated = Py_SIZE(self);
+                return NULL;
+            }
         }
     }
     Py_INCREF(Py_None);
@@ -1383,7 +1396,7 @@ array_tolist(arrayobject *self, PyObject *unused)
             Py_DECREF(list);
             return NULL;
         }
-        PyList_SetItem(list, i, v);
+        PyList_SET_ITEM(list, i, v);
     }
     return list;
 }
index 1e10dd708b99c038c842c4038a0ab096488babe8..02621358238ea76343a8ec84eb0fd08370f81922 100644 (file)
@@ -792,8 +792,7 @@ on_completion_display_matches_hook(char **matches,
         s = PyString_FromString(matches[i+1]);
         if (s == NULL)
             goto error;
-        if (PyList_SetItem(m, i, s) == -1)
-            goto error;
+        PyList_SET_ITEM(m, i, s);
     }
 
     r = PyObject_CallFunction(completion_display_matches_hook,
index 1dec6a120d05163cdb5bc4e659b28d642b792234..dc22f105fd71358b274bb15c07502f43c0af8f3a 100644 (file)
@@ -601,10 +601,7 @@ poll_poll(pollObject *self, PyObject *args)
                 goto error;
             }
             PyTuple_SET_ITEM(value, 1, num);
-            if ((PyList_SetItem(result_list, j, value)) == -1) {
-                Py_DECREF(value);
-                goto error;
-            }
+            PyList_SET_ITEM(result_list, j, value);
             i++;
         }
     }
index b561cd0f3039d7a11165f05868a73d936d5735b1..e1140a8e401243e10165c01af11d2573600bd229 100644 (file)
@@ -5247,7 +5247,7 @@ getarray(long a[256])
             Py_DECREF(l);
             return NULL;
         }
-        PyList_SetItem(l, i, x);
+        PyList_SET_ITEM(l, i, x);
     }
     for (i = 0; i < 256; i++)
         a[i] = 0;
@@ -5269,7 +5269,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
             Py_DECREF(l);
             return NULL;
         }
-        PyList_SetItem(l, i, x);
+        PyList_SET_ITEM(l, i, x);
     }
     return l;
 #endif
index b153ef63381bcf8c68866ac6e24e0fd87aed08cb..fdb7af2f5f6764879b9f61b70b4110883089691c 100644 (file)
@@ -1555,7 +1555,7 @@ makepathobject(char *path, int delim)
             Py_DECREF(v);
             return NULL;
         }
-        PyList_SetItem(v, i, w);
+        PyList_SET_ITEM(v, i, w);
         if (*p == '\0')
             break;
         path = p+1;