Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 17 additions & 9 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3870,36 +3870,42 @@ 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));
tuplesize = PyTuple_GET_SIZE(args);

/* 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);
Expand All @@ -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++) {
Expand All @@ -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;
}

Expand Down
Loading