Repository navigation
gh-163183: Fix crash in itertools.groupby when __eq__ consumes the current key - #163211
Open
abedalawieh wants to merge 1 commit into
Open
abedalawieh wants to merge 1 commit into
abedalawieh wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a key's
__eq__re-entersgroupbyduring the comparison ingroupby_next()and advances the new group (next(g)and thennext(grouper)),_grouper_next()clearsgbo->currkey. If__eq__then returns false, the loop breaks andPy_INCREF(gbo->currkey)dereferences NULL.The fix:
gbo->currkeywas cleared, the loop goes on togroupby_step()to fetch the next key, the same as it does at the top of the loop whencurrkeyis NULL.tgtkey, the grouper and the returned tuple. Releasing the oldtgtkeycan run arbitrary code (__del__) that could clearcurrkeyagain.The new test in
test_itertoolssegfaults under ASan without the fix. The reproducer from the issue now ends inStopIterationwithout crashing.test_itertoolsandtest_functoolspass on a--with-pydebug --with-address-sanitizerbuild.The same code is on 3.14 and 3.15, so this probably needs backports there.
itertools.groupby: NULL-pointer dereference (Py_INCREF(NULL)) ingroupby_next()when__eq__re-entrantly advances_grouper#163183