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

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().
This commit is contained in:
Zackery Spytz 2018-12-08 07:16:55 -07:00 committed by Serhiy Storchaka
parent f05df0a4b6
commit 99d56b5356
7 changed files with 25 additions and 30 deletions

View File

@ -71,20 +71,13 @@ copy_grouping(const char* s)
do { do {
i++; i++;
val = PyLong_FromLong(s[i]); val = PyLong_FromLong(s[i]);
if (!val) if (val == NULL) {
break; Py_DECREF(result);
if (PyList_SetItem(result, i, val)) { return NULL;
Py_DECREF(val);
val = NULL;
break;
} }
PyList_SET_ITEM(result, i, val);
} while (s[i] != '\0' && s[i] != CHAR_MAX); } while (s[i] != '\0' && s[i] != CHAR_MAX);
if (!val) {
Py_DECREF(result);
return NULL;
}
return result; return result;
} }

View File

@ -1544,12 +1544,18 @@ array_array_fromlist(arrayobject *self, PyObject *list)
if (array_resize(self, old_size + n) == -1) if (array_resize(self, old_size + n) == -1)
return NULL; return NULL;
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) {
array_resize(self, old_size); array_resize(self, old_size);
return NULL; return NULL;
} }
if (n != PyList_GET_SIZE(list)) {
PyErr_SetString(PyExc_RuntimeError,
"list changed size during iteration");
array_resize(self, old_size);
return NULL;
}
} }
} }
Py_RETURN_NONE; Py_RETURN_NONE;
@ -1574,8 +1580,7 @@ array_array_tolist_impl(arrayobject *self)
PyObject *v = getarrayitem((PyObject *)self, i); PyObject *v = getarrayitem((PyObject *)self, i);
if (v == NULL) if (v == NULL)
goto error; goto error;
if (PyList_SetItem(list, i, v) < 0) PyList_SET_ITEM(list, i, v);
goto error;
} }
return list; return list;

View File

@ -936,8 +936,7 @@ on_completion_display_matches_hook(char **matches,
s = decode(matches[i+1]); s = decode(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;
} }
sub = decode(matches[0]); sub = decode(matches[0]);
r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook, r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,

View File

@ -693,10 +693,7 @@ select_poll_poll_impl(pollObject *self, PyObject *timeout_obj)
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++;
} }
return result_list; return result_list;
@ -986,10 +983,7 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
Py_DECREF(num2); Py_DECREF(num2);
if (value == NULL) if (value == NULL)
goto error; goto error;
if ((PyList_SetItem(result_list, i, value)) == -1) { PyList_SET_ITEM(result_list, i, value);
Py_DECREF(value);
goto error;
}
} }
return result_list; return result_list;

View File

@ -756,9 +756,13 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
PyMem_Free(str); PyMem_Free(str);
return NULL; return NULL;
} }
PyList_SetItem(obData, PyObject *uni = PyUnicode_FromWideChar(str[index], len);
index, if (uni == NULL) {
PyUnicode_FromWideChar(str[index], len)); Py_DECREF(obData);
PyMem_Free(str);
return NULL;
}
PyList_SET_ITEM(obData, index, uni);
} }
PyMem_Free(str); PyMem_Free(str);

View File

@ -5124,7 +5124,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;
@ -5146,7 +5146,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

@ -2594,7 +2594,7 @@ makepathobject(const wchar_t *path, wchar_t 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;