Skip to content

Commit 89b5ea2

Browse files
[2.7] bpo-35441: Remove dead and buggy code related to PyList_SetItem(). (GH-11033) (GH-11234)
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 838645d commit 89b5ea2

File tree

6 files changed

+24
-22
lines changed

6 files changed

+24
-22
lines changed

Modules/_localemodule.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,13 @@ copy_grouping(char* s)
7373
do {
7474
i++;
7575
val = PyInt_FromLong(s[i]);
76-
if (!val)
77-
break;
78-
if (PyList_SetItem(result, i, val)) {
79-
Py_DECREF(val);
80-
val = NULL;
81-
break;
76+
if (val == NULL) {
77+
Py_DECREF(result);
78+
return NULL;
8279
}
80+
PyList_SET_ITEM(result, i, val);
8381
} while (s[i] != '\0' && s[i] != CHAR_MAX);
8482

85-
if (!val) {
86-
Py_DECREF(result);
87-
return NULL;
88-
}
89-
9083
return result;
9184
}
9285

Modules/arraymodule.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ array_fromlist(arrayobject *self, PyObject *list)
13441344
Py_SIZE(self) += n;
13451345
self->allocated = Py_SIZE(self);
13461346
for (i = 0; i < n; i++) {
1347-
PyObject *v = PyList_GetItem(list, i);
1347+
PyObject *v = PyList_GET_ITEM(list, i);
13481348
if ((*self->ob_descr->setitem)(self,
13491349
Py_SIZE(self) - n + i, v) != 0) {
13501350
Py_SIZE(self) -= n;
@@ -1357,6 +1357,19 @@ array_fromlist(arrayobject *self, PyObject *list)
13571357
self->allocated = Py_SIZE(self);
13581358
return NULL;
13591359
}
1360+
if (n != PyList_GET_SIZE(list)) {
1361+
PyErr_SetString(PyExc_RuntimeError,
1362+
"list changed size during iteration");
1363+
Py_SIZE(self) -= n;
1364+
if (itemsize && (Py_SIZE(self) > PY_SSIZE_T_MAX / itemsize)) {
1365+
return PyErr_NoMemory();
1366+
}
1367+
PyMem_RESIZE(item, char,
1368+
Py_SIZE(self) * itemsize);
1369+
self->ob_item = item;
1370+
self->allocated = Py_SIZE(self);
1371+
return NULL;
1372+
}
13601373
}
13611374
}
13621375
Py_INCREF(Py_None);
@@ -1383,7 +1396,7 @@ array_tolist(arrayobject *self, PyObject *unused)
13831396
Py_DECREF(list);
13841397
return NULL;
13851398
}
1386-
PyList_SetItem(list, i, v);
1399+
PyList_SET_ITEM(list, i, v);
13871400
}
13881401
return list;
13891402
}

Modules/readline.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,7 @@ on_completion_display_matches_hook(char **matches,
792792
s = PyString_FromString(matches[i+1]);
793793
if (s == NULL)
794794
goto error;
795-
if (PyList_SetItem(m, i, s) == -1)
796-
goto error;
795+
PyList_SET_ITEM(m, i, s);
797796
}
798797

799798
r = PyObject_CallFunction(completion_display_matches_hook,

Modules/selectmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,7 @@ poll_poll(pollObject *self, PyObject *args)
601601
goto error;
602602
}
603603
PyTuple_SET_ITEM(value, 1, num);
604-
if ((PyList_SetItem(result_list, j, value)) == -1) {
605-
Py_DECREF(value);
606-
goto error;
607-
}
604+
PyList_SET_ITEM(result_list, j, value);
608605
i++;
609606
}
610607
}

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5247,7 +5247,7 @@ getarray(long a[256])
52475247
Py_DECREF(l);
52485248
return NULL;
52495249
}
5250-
PyList_SetItem(l, i, x);
5250+
PyList_SET_ITEM(l, i, x);
52515251
}
52525252
for (i = 0; i < 256; i++)
52535253
a[i] = 0;
@@ -5269,7 +5269,7 @@ _Py_GetDXProfile(PyObject *self, PyObject *args)
52695269
Py_DECREF(l);
52705270
return NULL;
52715271
}
5272-
PyList_SetItem(l, i, x);
5272+
PyList_SET_ITEM(l, i, x);
52735273
}
52745274
return l;
52755275
#endif

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ makepathobject(char *path, int delim)
15551555
Py_DECREF(v);
15561556
return NULL;
15571557
}
1558-
PyList_SetItem(v, i, w);
1558+
PyList_SET_ITEM(v, i, w);
15591559
if (*p == '\0')
15601560
break;
15611561
path = p+1;

0 commit comments

Comments
 (0)