gh-146613: Fix re-entrant use-after-free in itertools._grouper#147962
gh-146613: Fix re-entrant use-after-free in itertools._grouper#147962TheSkyC wants to merge 8 commits intopython:mainfrom
Conversation
The same pattern was fixed in groupby.__next__ (pythongh-143543 / a91b5c3), but _grouper_next (the inner group iterator returned by groupby) was missed. A user-defined __eq__ can re-enter the grouper during PyObject_RichCompareBool, causing Py_XSETREF to free currkey while it is still being used. Fix by taking local snapshots of tgtkey/currkey + INCREF/DECREF protection, exactly as done in groupby_next. Added regression test in test_itertools.py.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Misc/NEWS.d/next/Library/2026-04-01-11-05-36.gh-issue-146613.GzjUFK.rst
Outdated
Show resolved
Hide resolved
|
Hi @vstinner, |
vstinner
left a comment
There was a problem hiding this comment.
LGTM.
I confirm that the test does crash without the fix, and does pass successfully with the fix.
Misc/NEWS.d/next/Library/2026-04-01-11-05-36.gh-issue-146613.GzjUFK.rst
Outdated
Show resolved
Hide resolved
…zjUFK.rst Co-authored-by: Victor Stinner <vstinner@python.org>
Misc/NEWS.d/next/Library/2026-04-01-11-05-36.gh-issue-146613.GzjUFK.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Head branch was pushed to by a user without write access
…zjUFK.rst Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
|
Lint CI job failed with: It's unrelated to this change. I reported the issue to: #145632 (comment). |
|
Thanks for reporting the issue, @vstinner! Should I update the branch from main to fix it? |
The issue is not fixed yet, so there is no need to update your branch. |
|
#147968 will fix the Lint CI. |
Closes gh-146613
The same pattern was fixed in
groupby.__next__(gh-143543 / a91b5c3), but_grouper_next(the inner group iterator returned bygroupby) was missed.A user-defined
__eq__can re-enter the grouper duringPyObject_RichCompareBool, causingPy_XSETREFto freecurrkeywhile it is still being used.Fixed by taking strong references (
Py_INCREF/Py_DECREF) totgtkeyandcurrkeybefore the comparison, exactly as done ingroupby_next.Added regression test
test_grouper_reentrant_eq_does_not_crash.