Commit 89b5ea29 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

[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 99d56b53)
Co-authored-by: default avatarZackery Spytz <zspytz@gmail.com>
parent 838645dc
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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,
......
......@@ -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++;
}
}
......
......@@ -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
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment