[Python-checkins] [2.7] bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033) (GH-11234)

Serhiy Storchaka webhook-mailer at python.org
Wed Dec 19 10:11:07 EST 2018


https://github.com/python/cpython/commit/89b5ea297d67f5efeb8fca0b63fa3d9f7030b2f0
commit: 89b5ea297d67f5efeb8fca0b63fa3d9f7030b2f0
branch: 2.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2018-12-19T17:11:02+02:00
summary:

[2.7] bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033) (GH-11234)

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 at gmail.com>

files:
M Modules/_localemodule.c
M Modules/arraymodule.c
M Modules/readline.c
M Modules/selectmodule.c
M Python/ceval.c
M Python/sysmodule.c

diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index 7e2f1a997be6..c55bd0d846eb 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -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;
 }
 
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 5bd3a42f009d..8d4eb0932df5 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -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;
 }
diff --git a/Modules/readline.c b/Modules/readline.c
index 1e10dd708b99..02621358238e 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -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,
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 1dec6a120d05..dc22f105fd71 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -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++;
         }
     }
diff --git a/Python/ceval.c b/Python/ceval.c
index b561cd0f3039..e1140a8e4012 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index b153ef63381b..fdb7af2f5f67 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -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;



More information about the Python-checkins mailing list