[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 99d56b5356)

Co-authored-by: Zackery Spytz <zspytz@gmail.com>
This commit is contained in:
Serhiy Storchaka 2018-12-19 17:11:02 +02:00 committed by GitHub
parent 838645dc41
commit 89b5ea297d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 22 deletions

View File

@ -73,19 +73,12 @@ copy_grouping(char* s)
do { do {
i++; i++;
val = PyInt_FromLong(s[i]); val = PyInt_FromLong(s[i]);
if (!val) if (val == NULL) {
break;
if (PyList_SetItem(result, i, val)) {
Py_DECREF(val);
val = NULL;
break;
}
} while (s[i] != '\0' && s[i] != CHAR_MAX);
if (!val) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} }
PyList_SET_ITEM(result, i, val);
} while (s[i] != '\0' && s[i] != CHAR_MAX);
return result; return result;
} }

View File

@ -1344,7 +1344,7 @@ array_fromlist(arrayobject *self, PyObject *list)
Py_SIZE(self) += n; Py_SIZE(self) += n;
self->allocated = Py_SIZE(self); self->allocated = Py_SIZE(self);
for (i = 0; i < n; i++) { 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, if ((*self->ob_descr->setitem)(self,
Py_SIZE(self) - n + i, v) != 0) { Py_SIZE(self) - n + i, v) != 0) {
Py_SIZE(self) -= n; Py_SIZE(self) -= n;
@ -1357,6 +1357,19 @@ array_fromlist(arrayobject *self, PyObject *list)
self->allocated = Py_SIZE(self); self->allocated = Py_SIZE(self);
return NULL; 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); Py_INCREF(Py_None);
@ -1383,7 +1396,7 @@ array_tolist(arrayobject *self, PyObject *unused)
Py_DECREF(list); Py_DECREF(list);
return NULL; return NULL;
} }
PyList_SetItem(list, i, v); PyList_SET_ITEM(list, i, v);
} }
return list; return list;
} }

View File

@ -792,8 +792,7 @@ on_completion_display_matches_hook(char **matches,
s = PyString_FromString(matches[i+1]); s = PyString_FromString(matches[i+1]);
if (s == NULL) if (s == NULL)
goto error; goto error;
if (PyList_SetItem(m, i, s) == -1) PyList_SET_ITEM(m, i, s);
goto error;
} }
r = PyObject_CallFunction(completion_display_matches_hook, r = PyObject_CallFunction(completion_display_matches_hook,

View File

@ -601,10 +601,7 @@ poll_poll(pollObject *self, PyObject *args)
goto error; goto error;
} }
PyTuple_SET_ITEM(value, 1, num); PyTuple_SET_ITEM(value, 1, num);
if ((PyList_SetItem(result_list, j, value)) == -1) { PyList_SET_ITEM(result_list, j, value);
Py_DECREF(value);
goto error;
}
i++; i++;
} }
} }

View File

@ -5247,7 +5247,7 @@ getarray(long a[256])
Py_DECREF(l); Py_DECREF(l);
return NULL; return NULL;
} }
PyList_SetItem(l, i, x); PyList_SET_ITEM(l, i, x);
} }
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
a[i] = 0; a[i] = 0;
@ -5269,7 +5269,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
Py_DECREF(l); Py_DECREF(l);
return NULL; return NULL;
} }
PyList_SetItem(l, i, x); PyList_SET_ITEM(l, i, x);
} }
return l; return l;
#endif #endif

View File

@ -1555,7 +1555,7 @@ makepathobject(char *path, int delim)
Py_DECREF(v); Py_DECREF(v);
return NULL; return NULL;
} }
PyList_SetItem(v, i, w); PyList_SET_ITEM(v, i, w);
if (*p == '\0') if (*p == '\0')
break; break;
path = p+1; path = p+1;