gh-149676: fix: properly initialise frozendict hash value#149675
Open
KowalskiThomas wants to merge 3 commits into
Open
gh-149676: fix: properly initialise frozendict hash value#149675KowalskiThomas wants to merge 3 commits into
frozendict hash value#149675KowalskiThomas wants to merge 3 commits into
Conversation
frozendict hash valuefrozendict hash value
vstinner
reviewed
May 11, 2026
| if (result != NULL) { | ||
| /* ma_hash must be -1 (sentinel for "not computed") since PyObject_GC_New | ||
| does not zero-initialize memory and new_dict_impl does not touch ma_hash. */ | ||
| _PyFrozenDictObject_CAST(result)->ma_hash = -1; |
Member
There was a problem hiding this comment.
Can you move this assignment to new_dict_impl()? Adding an int frozendict parameter.
| PyObject *result = new_dict_impl(mp, keys, values, used, free_values_on_failure); | ||
| if (result != NULL) { | ||
| /* ma_hash must be -1 (sentinel for "not computed") since PyObject_GC_New | ||
| does not zero-initialize memory and new_dict_impl does not touch ma_hash. */ |
Member
There was a problem hiding this comment.
I don't think that this comment is useful.
| with self.assertRaisesRegex(TypeError, "unhashable type: 'list'"): | ||
| hash(fd) | ||
|
|
||
| def test_hash_pipe_operator(self): |
Member
There was a problem hiding this comment.
You should move this test to existing test_merge().
Comment on lines
+1906
to
+1914
| # gh-149676: frozendict created via | must have the same hash as one | ||
| # created directly with the same contents (ma_hash must be initialised | ||
| # to -1 so that the hash is computed on first call, not left as garbage) | ||
| a = frozendict({"a": 1}) | ||
| b = frozendict({"b": 2}) | ||
| c = frozendict({"a": 1, "b": 2}) | ||
| c_union = a | b | ||
| self.assertEqual(c, c_union) | ||
| self.assertEqual(hash(c), hash(c_union)) |
Member
There was a problem hiding this comment.
The self.assertEqual(c, c_union) test is redundant with test_merge() tests. I suggest to simplify the test to:
Suggested change
| # gh-149676: frozendict created via | must have the same hash as one | |
| # created directly with the same contents (ma_hash must be initialised | |
| # to -1 so that the hash is computed on first call, not left as garbage) | |
| a = frozendict({"a": 1}) | |
| b = frozendict({"b": 2}) | |
| c = frozendict({"a": 1, "b": 2}) | |
| c_union = a | b | |
| self.assertEqual(c, c_union) | |
| self.assertEqual(hash(c), hash(c_union)) | |
| # gh-149676: Test hash(frozendict | frozendict) | |
| a = frozendict({"a": 1}) | |
| b = frozendict({"b": 2}) | |
| self.assertEqual(hash(a | b), hash(frozendict({"a": 1, "b": 2}))) |
| @@ -0,0 +1 @@ | |||
| Fix :class:`frozendict` instances being created with an uninitialised hash value, which could cause incorrect hash lookups or crashes. | |||
Member
There was a problem hiding this comment.
The issue cannot lead to a crash. I suggest:
Fix frozendict | frozendict hash.
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.
frozendict's hash is not initialised when created through pipe operator #149676