https://github.com/python/cpython/commit/c8746492c580a33b2c903ac7b7ba78e43788bdd6
commit: c8746492c580a33b2c903ac7b7ba78e43788bdd6
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-20T10:26:52-07:00
summary:

gh-159739: Handle OrderedDict item iterator allocation failures (#159774)



Co-authored-by: Jelle Zijlstra <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst
M Objects/odictobject.c

diff --git 
a/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst 
b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst
new file mode 100644
index 00000000000000..e7eac98810cf48
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-10-19-20-38-17.gh-issue-159739.5a6e94.rst
@@ -0,0 +1,2 @@
+Avoid a crash when memory allocation fails while creating a
+:class:`collections.OrderedDict` items iterator.
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index a860ade3b148e5..5eaeaaf87a5856 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1941,22 +1941,21 @@ odictiter_new(PyODictObject *od, int kind)
     odictiterobject *di;
     _ODictNode *node;
     int reversed = kind & _odict_ITER_REVERSED;
-
-    di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
-    if (di == NULL)
-        return NULL;
+    PyObject *result = NULL;
 
     if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS) {
-        di->di_result = _PyTuple_FromPairSteal(Py_None, Py_None);
-        if (di->di_result == NULL) {
-            Py_DECREF(di);
+        result = _PyTuple_FromPairSteal(Py_None, Py_None);
+        if (result == NULL) {
             return NULL;
         }
     }
-    else {
-        di->di_result = NULL;
+    di = PyObject_GC_New(odictiterobject, &PyODictIter_Type);
+    if (di == NULL) {
+        Py_XDECREF(result);
+        return NULL;
     }
 
+    di->di_result = result;
     di->kind = kind;
     Py_BEGIN_CRITICAL_SECTION(od);
     node = reversed ? _odict_LAST(od) : _odict_FIRST(od);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to