Repository navigation
gh-142637: Fix use-after-free in OrderedDict lookups with a re-entrant __eq__ - #160275
Closed
AnKiT-GaRG2 wants to merge 1 commit into
Closed
AnKiT-GaRG2 wants to merge 1 commit into
AnKiT-GaRG2 wants to merge 1 commit into
Conversation
…entrant __eq__ Looking up a key can call its __eq__ method, which may clear the dictionary and so replace its keys. _odict_get_index_raw() then read the freed keys, and _odict_get_index() used an od_fast_nodes table that no longer matched them. Look the key up again when the keys were replaced, as dict does.
Member
|
Don't open PRs if the issue is still being discussed especially not if they are simply LLM generated. |
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.
Fixes the heap-use-after-free reported in #142637 for
del od[key],od.pop(key)andod.move_to_end(key).Looking up a key can call its
__eq__, which may callod.clear()and so replacema_keys:_odict_get_index_raw()readdk_nentriesfrom akeyspointer cached before the lookup, which was freed by then._odict_get_index()returned an index into anod_fast_nodestable sized for the old keys._odict_get_index()now looks the key up again when the keys were replaced during the lookup, which is whatdictdoes, and_odict_get_index_raw()reads the current keys. The operations then behave like theirdictcounterparts: the key is not found, soKeyErroris raised.In the issue, @picnixz suggested either matching
dictor raisingRuntimeErroras in gh-121329. I went with matchingdictbecause it needs no new exception, but I can switch toRuntimeErrorif @rhettinger prefers that.Tested with a debug build and a
--with-address-sanitizerbuild: the three reproducers from the issue report a heap-use-after-free in_odict_get_index_rawwithout the change, and none with it.test_ordered_dict,test_dictandtest_collectionspass under ASAN.OrderedDictoperations via re-entrant__eq__#142637