Skip to content

Commit 8b7d8ac

Browse files
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 99d56b5) Co-authored-by: Zackery Spytz <[email protected]>
1 parent ee2c5a8 commit 8b7d8ac

File tree

7 files changed

+25
-30
lines changed

7 files changed

+25
-30
lines changed

Modules/_localemodule.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,13 @@ copy_grouping(const char* s)
7070
do {
7171
i++;
7272
val = PyLong_FromLong(s[i]);
73-
if (!val)
74-
break;
75-
if (PyList_SetItem(result, i, val)) {
76-
Py_DECREF(val);
77-
val = NULL;
78-
break;
73+
if (val == NULL) {
74+
Py_DECREF(result);
75+
return NULL;
7976
}
77+
PyList_SET_ITEM(result, i, val);
8078
} while (s[i] != '\0' && s[i] != CHAR_MAX);
8179

82-
if (!val) {
83-
Py_DECREF(result);
84-
return NULL;
85-
}
86-
8780
return result;
8881
}
8982

Modules/arraymodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,12 +1544,18 @@ array_array_fromlist(arrayobject *self, PyObject *list)
15441544
if (array_resize(self, old_size + n) == -1)
15451545
return NULL;
15461546
for (i = 0; i < n; i++) {
1547-
PyObject *v = PyList_GetItem(list, i);
1547+
PyObject *v = PyList_GET_ITEM(list, i);
15481548
if ((*self->ob_descr->setitem)(self,
15491549
Py_SIZE(self) - n + i, v) != 0) {
15501550
array_resize(self, old_size);
15511551
return NULL;
15521552
}
1553+
if (n != PyList_GET_SIZE(list)) {
1554+
PyErr_SetString(PyExc_RuntimeError,
1555+
"list changed size during iteration");
1556+
array_resize(self, old_size);
1557+
return NULL;
1558+
}
15531559
}
15541560
}
15551561
Py_RETURN_NONE;
@@ -1574,8 +1580,7 @@ array_array_tolist_impl(arrayobject *self)
15741580
PyObject *v = getarrayitem((PyObject *)self, i);
15751581
if (v == NULL)
15761582
goto error;
1577-
if (PyList_SetItem(list, i, v) < 0)
1578-
goto error;
1583+
PyList_SET_ITEM(list, i, v);
15791584
}
15801585
return list;
15811586

Modules/readline.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,7 @@ on_completion_display_matches_hook(char **matches,
936936
s = decode(matches[i+1]);
937937
if (s == NULL)
938938
goto error;
939-
if (PyList_SetItem(m, i, s) == -1)
940-
goto error;
939+
PyList_SET_ITEM(m, i, s);
941940
}
942941
sub = decode(matches[0]);
943942
r = PyObject_CallFunction(readlinestate_global->completion_display_matches_hook,

Modules/selectmodule.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,7 @@ poll_poll(pollObject *self, PyObject *args)
652652
goto error;
653653
}
654654
PyTuple_SET_ITEM(value, 1, num);
655-
if ((PyList_SetItem(result_list, j, value)) == -1) {
656-
Py_DECREF(value);
657-
goto error;
658-
}
655+
PyList_SET_ITEM(result_list, j, value);
659656
i++;
660657
}
661658
return result_list;
@@ -981,10 +978,7 @@ devpoll_poll(devpollObject *self, PyObject *args)
981978
Py_DECREF(num2);
982979
if (value == NULL)
983980
goto error;
984-
if ((PyList_SetItem(result_list, i, value)) == -1) {
985-
Py_DECREF(value);
986-
goto error;
987-
}
981+
PyList_SET_ITEM(result_list, i, value);
988982
}
989983

990984
return result_list;

PC/winreg.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,13 @@ Reg2Py(BYTE *retDataBuf, DWORD retDataSize, DWORD typ)
757757
PyMem_Free(str);
758758
return NULL;
759759
}
760-
PyList_SetItem(obData,
761-
index,
762-
PyUnicode_FromWideChar(str[index], len));
760+
PyObject *uni = PyUnicode_FromWideChar(str[index], len);
761+
if (uni == NULL) {
762+
Py_DECREF(obData);
763+
PyMem_Free(str);
764+
return NULL;
765+
}
766+
PyList_SET_ITEM(obData, index, uni);
763767
}
764768
PyMem_Free(str);
765769

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5056,7 +5056,7 @@ getarray(long a[256])
50565056
Py_DECREF(l);
50575057
return NULL;
50585058
}
5059-
PyList_SetItem(l, i, x);
5059+
PyList_SET_ITEM(l, i, x);
50605060
}
50615061
for (i = 0; i < 256; i++)
50625062
a[i] = 0;
@@ -5078,7 +5078,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
50785078
Py_DECREF(l);
50795079
return NULL;
50805080
}
5081-
PyList_SetItem(l, i, x);
5081+
PyList_SET_ITEM(l, i, x);
50825082
}
50835083
return l;
50845084
#endif

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ makepathobject(const wchar_t *path, wchar_t delim)
25442544
Py_DECREF(v);
25452545
return NULL;
25462546
}
2547-
PyList_SetItem(v, i, w);
2547+
PyList_SET_ITEM(v, i, w);
25482548
if (*p == '\0')
25492549
break;
25502550
path = p+1;

0 commit comments

Comments
 (0)