Skip to content

Commit 25555e0

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 12b9fb6 commit 25555e0

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
@@ -1499,12 +1499,18 @@ array_array_fromlist(arrayobject *self, PyObject *list)
14991499
if (array_resize(self, old_size + n) == -1)
15001500
return NULL;
15011501
for (i = 0; i < n; i++) {
1502-
PyObject *v = PyList_GetItem(list, i);
1502+
PyObject *v = PyList_GET_ITEM(list, i);
15031503
if ((*self->ob_descr->setitem)(self,
15041504
Py_SIZE(self) - n + i, v) != 0) {
15051505
array_resize(self, old_size);
15061506
return NULL;
15071507
}
1508+
if (n != PyList_GET_SIZE(list)) {
1509+
PyErr_SetString(PyExc_RuntimeError,
1510+
"list changed size during iteration");
1511+
array_resize(self, old_size);
1512+
return NULL;
1513+
}
15081514
}
15091515
}
15101516
Py_INCREF(Py_None);
@@ -1530,8 +1536,7 @@ array_array_tolist_impl(arrayobject *self)
15301536
PyObject *v = getarrayitem((PyObject *)self, i);
15311537
if (v == NULL)
15321538
goto error;
1533-
if (PyList_SetItem(list, i, v) < 0)
1534-
goto error;
1539+
PyList_SET_ITEM(list, i, v);
15351540
}
15361541
return list;
15371542

Modules/readline.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,7 @@ on_completion_display_matches_hook(char **matches,
947947
s = decode(matches[i+1]);
948948
if (s == NULL)
949949
goto error;
950-
if (PyList_SetItem(m, i, s) == -1)
951-
goto error;
950+
PyList_SET_ITEM(m, i, s);
952951
}
953952
sub = decode(matches[0]);
954953
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
@@ -655,10 +655,7 @@ poll_poll(pollObject *self, PyObject *args)
655655
goto error;
656656
}
657657
PyTuple_SET_ITEM(value, 1, num);
658-
if ((PyList_SetItem(result_list, j, value)) == -1) {
659-
Py_DECREF(value);
660-
goto error;
661-
}
658+
PyList_SET_ITEM(result_list, j, value);
662659
i++;
663660
}
664661
return result_list;
@@ -984,10 +981,7 @@ devpoll_poll(devpollObject *self, PyObject *args)
984981
Py_DECREF(num2);
985982
if (value == NULL)
986983
goto error;
987-
if ((PyList_SetItem(result_list, i, value)) == -1) {
988-
Py_DECREF(value);
989-
goto error;
990-
}
984+
PyList_SET_ITEM(result_list, i, value);
991985
}
992986

993987
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
@@ -5500,7 +5500,7 @@ getarray(long a[256])
55005500
Py_DECREF(l);
55015501
return NULL;
55025502
}
5503-
PyList_SetItem(l, i, x);
5503+
PyList_SET_ITEM(l, i, x);
55045504
}
55055505
for (i = 0; i < 256; i++)
55065506
a[i] = 0;
@@ -5522,7 +5522,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
55225522
Py_DECREF(l);
55235523
return NULL;
55245524
}
5525-
PyList_SetItem(l, i, x);
5525+
PyList_SET_ITEM(l, i, x);
55265526
}
55275527
return l;
55285528
#endif

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ makepathobject(const wchar_t *path, wchar_t delim)
21092109
Py_DECREF(v);
21102110
return NULL;
21112111
}
2112-
PyList_SetItem(v, i, w);
2112+
PyList_SET_ITEM(v, i, w);
21132113
if (*p == '\0')
21142114
break;
21152115
path = p+1;

0 commit comments

Comments
 (0)