You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
itertools.groupby: use-after-free in _grouper_next() when __eq__ re-enters the iterator #145678
_grouper_next() in Modules/itertoolsmodule.c passes igo->tgtkey and gbo->currkey directly to PyObject_RichCompareBool() without holding strong references to them first.
A user-defined __eq__ method can re-enter the parent groupby iterator during that comparison. Re-entry calls groupby_step(), which contains:
Py_XSETREF(gbo->currkey, newkey);
This frees gbo->currkey while it is still being compared — a use-after-free.
gh-143543 fixed the same bug in groupby_next() by taking INCREF'd local snapshots before calling PyObject_RichCompareBool(). The fix was not applied to the analogous code in _grouper_next().
Fix
Apply the same INCREF snapshot pattern used in the groupby_next() fix:
I cannot reproduce the crash with the given reproducer actually, but I do think the path could be problematic at first glance. We just need to find a good reproducer.
Activity
pythongh-145678: Fix use-after-free in itertools.groupby _grouper_next()
pythongh-145678: Fix use-after-free in itertools.groupby _grouper_next()
pythongh-145678: Simplify test_grouper_next_reentrant_eq_does_not_crash
pythongh-145678: Remove unnecessary try/except in test
Merge branch 'main' into pythongh-145678-grouper-uaf
pythongh-145678: Update NEWS entry wording per review
Merge branch 'main' into pythongh-145678-grouper-uaf
pythongh-145678: Strengthen test to assert refcount invariant
pythongh-145678: Guard against re-entrant __eq__ never being called
pythongh-145678: Revert sys.getrefcount assertions from test
picnixz commentedon Mar 14, 2026
I cannot reproduce the crash with the given reproducer actually, but I do think the path could be problematic at first glance. We just need to find a good reproducer.
itertools._grouper.__next__has the same re-entrant use-after-free that was fixed ingroupby.__next__#146613 as a duplicate of this issueitertools._grouper.__next__has the same re-entrant use-after-free that was fixed ingroupby.__next__#146613 as a duplicate of this issuepicnixz commentedon Mar 29, 2026
Closing because this one doesn't have a proper reproducer while the other has.