Skip to content

Commit 4755bea

Browse files
committed
Issue #18408: Fix array_tolist(), handle PyList_SetItem() failure
1 parent 0b142e2 commit 4755bea

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Modules/arraymodule.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ array_contains(arrayobject *self, PyObject *v)
10271027
for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(self); i++) {
10281028
PyObject *selfi = getarrayitem((PyObject *)self, i);
10291029
if (selfi == NULL)
1030-
return NULL;
1030+
return -1;
10311031
cmp = PyObject_RichCompareBool(selfi, v, Py_EQ);
10321032
Py_DECREF(selfi);
10331033
}
@@ -1405,13 +1405,16 @@ array_tolist(arrayobject *self, PyObject *unused)
14051405
return NULL;
14061406
for (i = 0; i < Py_SIZE(self); i++) {
14071407
PyObject *v = getarrayitem((PyObject *)self, i);
1408-
if (v == NULL) {
1409-
Py_DECREF(list);
1410-
return NULL;
1411-
}
1412-
PyList_SetItem(list, i, v);
1408+
if (v == NULL)
1409+
goto error;
1410+
if (PyList_SetItem(list, i, v) < 0)
1411+
goto error;
14131412
}
14141413
return list;
1414+
1415+
error:
1416+
Py_DECREF(list);
1417+
return NULL;
14151418
}
14161419

14171420
PyDoc_STRVAR(tolist_doc,

0 commit comments

Comments
 (0)