[Python-checkins] bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033)
Miss Islington (bot)
webhook-mailer at python.org
Sat Dec 8 09:39:41 EST 2018
https://github.com/python/cpython/commit/25555e0fbed15f809a247c7e16ab9d0a0088f806
commit: 25555e0fbed15f809a247c7e16ab9d0a0088f806
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018年12月08日T06:39:37-08:00
summary:
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().
(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 PC/winreg.c
M Python/ceval.c
M Python/sysmodule.c
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index f3421af65f3d..eba8289c301b 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -70,20 +70,13 @@ copy_grouping(const char* s)
do {
i++;
val = PyLong_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 9254af59a6a2..bc3f62def3cc 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1499,12 +1499,18 @@ array_array_fromlist(arrayobject *self, PyObject *list)
if (array_resize(self, old_size + n) == -1)
return NULL;
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) {
array_resize(self, old_size);
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_INCREF(Py_None);
@@ -1530,8 +1536,7 @@ array_array_tolist_impl(arrayobject *self)
PyObject *v = getarrayitem((PyObject *)self, i);
if (v == NULL)
goto error;
- if (PyList_SetItem(list, i, v) < 0)
- goto error;
+ PyList_SET_ITEM(list, i, v);
}
return list;
diff --git a/Modules/readline.c b/Modules/readline.c
index 575479cff249..096571548c53 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -947,8 +947,7 @@ on_completion_display_matches_hook(char **matches,
s = decode(matches[i+1]);
if (s == NULL)
goto error;
- if (PyList_SetItem(m, i, s) == -1)
- goto error;
+ PyList_SET_ITEM(m, i, s);
}
sub = decode(matches[0]);
r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 4b9965724915..2bc7b9ff79e5 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -655,10 +655,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++;
}
return result_list;
@@ -984,10 +981,7 @@ devpoll_poll(devpollObject *self, PyObject *args)
Py_DECREF(num2);
if (value == NULL)
goto error;
- if ((PyList_SetItem(result_list, i, value)) == -1) {
- Py_DECREF(value);
- goto error;
- }
+ PyList_SET_ITEM(result_list, i, value);
}
return result_list;
diff --git a/PC/winreg.c b/PC/winreg.c
index 5efdc5e0efec..3fde04d746b5 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -757,9 +757,13 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
PyMem_Free(str);
return NULL;
}
- PyList_SetItem(obData,
- index,
- PyUnicode_FromWideChar(str[index], len));
+ PyObject *uni = PyUnicode_FromWideChar(str[index], len);
+ if (uni == NULL) {
+ Py_DECREF(obData);
+ PyMem_Free(str);
+ return NULL;
+ }
+ PyList_SET_ITEM(obData, index, uni);
}
PyMem_Free(str);
diff --git a/Python/ceval.c b/Python/ceval.c
index 50834f82c746..38d1d73845fb 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5500,7 +5500,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;
@@ -5522,7 +5522,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 b09268b0e538..7d1493cbe145 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -2109,7 +2109,7 @@ makepathobject(const wchar_t *path, wchar_t 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