From 89b5ea297d67f5efeb8fca0b63fa3d9f7030b2f0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 19 Dec 2018 17:11:02 +0200 Subject: [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 --- Modules/_localemodule.c | 15 ++++----------- Modules/arraymodule.c | 17 +++++++++++++++-- Modules/readline.c | 3 +-- Modules/selectmodule.c | 5 +---- Python/ceval.c | 4 ++-- Python/sysmodule.c | 2 +- 6 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c index 7e2f1a997b..c55bd0d846 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 5bd3a42f00..8d4eb0932d 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 1e10dd708b..0262135823 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 1dec6a120d..dc22f105fd 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 b561cd0f30..e1140a8e40 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 b153ef6338..fdb7af2f5f 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; -- cgit v1.2.1