Skip to content

Commit c874649

Browse files
gh-159739: Handle OrderedDict item iterator allocation failures (#159774)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 905aab6 commit c874649

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid a crash when memory allocation fails while creating a
2+
:class:`collections.OrderedDict` items iterator.

‎Objects/odictobject.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,22 +1941,21 @@ odictiter_new(PyODictObject *od, int kind)
19411941
odictiterobject *di;
19421942
_ODictNode *node;
19431943
int reversed = kind & _odict_ITER_REVERSED;
1944-
1945-
di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
1946-
if (di == NULL)
1947-
return NULL;
1944+
PyObject *result = NULL;
19481945

19491946
if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
1950-
di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);
1951-
if (di->di_result == NULL) {
1952-
Py_DECREF(di);
1947+
result = _PyTuple_FromPairSteal(Py_None, Py_None);
1948+
if (result == NULL) {
19531949
return NULL;
19541950
}
19551951
}
1956-
else {
1957-
di->di_result = NULL;
1952+
di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
1953+
if (di == NULL) {
1954+
Py_XDECREF(result);
1955+
return NULL;
19581956
}
19591957

1958+
di->di_result = result;
19601959
di->kind = kind;
19611960
Py_BEGIN_CRITICAL_SECTION(od);
19621961
node = reversed ? _odict_LAST(od) : _odict_FIRST(od);

0 commit comments

Comments
 (0)