Skip to content

Commit 99d56b5

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
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().
1 parent f05df0a commit 99d56b5

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
@@ -71,20 +71,13 @@ copy_grouping(const char* s)
7171
do {
7272
i++;
7373
val = PyLong_FromLong(s[i]);
74-
if (!val)
75-
break;
76-
if (PyList_SetItem(result, i, val)) {
77-
Py_DECREF(val);
78-
val = NULL;
79-
break;
74+
if (val == NULL) {
75+
Py_DECREF(result);
76+
return NULL;
8077
}
78+
PyList_SET_ITEM(result, i, val);
8179
} while (s[i] != '\0' && s[i] != CHAR_MAX);
8280

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

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
@@ -693,10 +693,7 @@ select_poll_poll_impl(pollObject *self, PyObject *timeout_obj)
693693
goto error;
694694
}
695695
PyTuple_SET_ITEM(value, 1, num);
696-
if ((PyList_SetItem(result_list, j, value)) == -1) {
697-
Py_DECREF(value);
698-
goto error;
699-
}
696+
PyList_SET_ITEM(result_list, j, value);
700697
i++;
701698
}
702699
return result_list;
@@ -986,10 +983,7 @@ select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj)
986983
Py_DECREF(num2);
987984
if (value == NULL)
988985
goto error;
989-
if ((PyList_SetItem(result_list, i, value)) == -1) {
990-
Py_DECREF(value);
991-
goto error;
992-
}
986+
PyList_SET_ITEM(result_list, i, value);
993987
}
994988

995989
return result_list;

PC/winreg.c

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

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,7 +5124,7 @@ getarray(long a[256])
51245124
Py_DECREF(l);
51255125
return NULL;
51265126
}
5127-
PyList_SetItem(l, i, x);
5127+
PyList_SET_ITEM(l, i, x);
51285128
}
51295129
for (i = 0; i < 256; i++)
51305130
a[i] = 0;
@@ -5146,7 +5146,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
51465146
Py_DECREF(l);
51475147
return NULL;
51485148
}
5149-
PyList_SetItem(l, i, x);
5149+
PyList_SET_ITEM(l, i, x);
51505150
}
51515151
return l;
51525152
#endif

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ makepathobject(const wchar_t *path, wchar_t delim)
25942594
Py_DECREF(v);
25952595
return NULL;
25962596
}
2597-
PyList_SetItem(v, i, w);
2597+
PyList_SET_ITEM(v, i, w);
25982598
if (*p == '\0')
25992599
break;
26002600
path = p+1;

0 commit comments

Comments
 (0)