From 40a7b6482c5822bfea00b41970fee34a07417a1b Mon Sep 17 00:00:00 2001 From: espressolee <70549809+espressolee@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:49:22 +0800 Subject: [PATCH] gh-162273: Acquire strong reference to fillvalue upfront in itertools.zip_longest zip_longest_new() borrowed fillvalue from kwds via PyDict_GetItemWithError() and only promoted it with Py_NewRef() after calling PyObject_GetIter() on input iterables. Use PyDict_GetItemRef() upfront and decref fillvalue on error paths so clearing or mutating kwds during iterator initialization cannot free fillvalue before ownership is stored on the zip_longest object. --- Lib/test/test_itertools.py | 42 +++++++++++++++++++ ...-11-19-14-38-00.gh-issue-162273.7K9M2P.rst | 2 + Modules/itertoolsmodule.c | 26 ++++++++---- 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-19-14-38-00.gh-issue-162273.7K9M2P.rst diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index d47f9acf019dca..c46bec0d6f00fa 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -908,6 +908,48 @@ def __iter__(self): self.assertIs(cm.exception, exception) + @threading_helper.requires_working_threading() + def test_zip_longest_reentrant_kwds_clear(self): + # gh-162273: clearing kwds inside __iter__ must not deallocate + # fillvalue before zip_longest_new acquires a strong reference. + import _thread + + events = [] + cleared = threading.Event() + merged = threading.Event() + finished = threading.Event() + + class EphemeralFillValue: + def __del__(self): + events.append("DEALLOCATED") + + class Trigger: + def __init__(self, kw): + self.kw = kw + def __iter__(self): + events.append("BEFORE_CLEAR") + self.kw.clear() + cleared.set() + merged.wait(timeout=support.SHORT_TIMEOUT) + events.append("RETURNING_FROM_GETITER") + return iter([1]) + + class TailIterable: + def __iter__(self): + finished.set() + return iter([2]) + + kw = {"fillvalue": EphemeralFillValue()} + with threading_helper.wait_threads_exit(): + _thread.start_new_thread(zip_longest, (Trigger(kw), TailIterable()), kw) + self.assertTrue(cleared.wait(timeout=support.SHORT_TIMEOUT)) + for _ in range(1000): + pass + merged.set() + self.assertTrue(finished.wait(timeout=support.SHORT_TIMEOUT)) + + self.assertEqual(events[:2], ["BEFORE_CLEAR", "RETURNING_FROM_GETITER"]) + def test_bug_7244(self): class Repeater: diff --git a/Misc/NEWS.d/next/Library/2026-11-19-14-38-00.gh-issue-162273.7K9M2P.rst b/Misc/NEWS.d/next/Library/2026-11-19-14-38-00.gh-issue-162273.7K9M2P.rst new file mode 100644 index 00000000000000..2b8a879b60c48f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-14-38-00.gh-issue-162273.7K9M2P.rst @@ -0,0 +1,2 @@ +Fix a use-after-free in :class:`itertools.zip_longest` when the ``kwds`` +dictionary is mutated or cleared while initializing input iterators. diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index c0023c839ca7fe..257215036bedaa 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -3870,22 +3870,25 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_ssize_t i; PyObject *ittuple; /* tuple of iterators */ PyObject *result; - PyObject *fillvalue = Py_None; + PyObject *fillvalue = NULL; Py_ssize_t tuplesize; if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_GET_SIZE(kwds) > 0) { - fillvalue = NULL; if (PyDict_GET_SIZE(kwds) == 1) { - fillvalue = PyDict_GetItemWithError(kwds, &_Py_ID(fillvalue)); + int rc = PyDict_GetItemRef(kwds, &_Py_ID(fillvalue), &fillvalue); + if (rc < 0) { + return NULL; + } } if (fillvalue == NULL) { - if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_TypeError, - "zip_longest() got an unexpected keyword argument"); - } + PyErr_SetString(PyExc_TypeError, + "zip_longest() got an unexpected keyword argument"); return NULL; } } + else { + fillvalue = Py_NewRef(Py_None); + } /* args must be a tuple */ assert(PyTuple_Check(args)); @@ -3893,13 +3896,16 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) /* obtain iterators */ ittuple = PyTuple_New(tuplesize); - if (ittuple == NULL) + if (ittuple == NULL) { + Py_DECREF(fillvalue); return NULL; + } for (i=0; i < tuplesize; i++) { PyObject *item = PyTuple_GET_ITEM(args, i); PyObject *it = PyObject_GetIter(item); if (it == NULL) { Py_DECREF(ittuple); + Py_DECREF(fillvalue); return NULL; } PyTuple_SET_ITEM(ittuple, i, it); @@ -3909,6 +3915,7 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) result = PyTuple_New(tuplesize); if (result == NULL) { Py_DECREF(ittuple); + Py_DECREF(fillvalue); return NULL; } for (i=0 ; i < tuplesize ; i++) { @@ -3921,13 +3928,14 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (lz == NULL) { Py_DECREF(ittuple); Py_DECREF(result); + Py_DECREF(fillvalue); return NULL; } lz->ittuple = ittuple; lz->tuplesize = tuplesize; lz->numactive = tuplesize; lz->result = result; - lz->fillvalue = Py_NewRef(fillvalue); + lz->fillvalue = fillvalue; return (PyObject *)lz; }