-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-154318: Guard C recursion when hashing nested tuples and frozendicts #154362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1919,6 +1919,18 @@ def test_hash(self): | |
| with self.assertRaisesRegex(TypeError, "unhashable type: 'list'"): | ||
| hash(fd) | ||
|
|
||
| def test_hash_deeply_nested(self): | ||
| # This should raise a RecursionError and not crash. | ||
| # Nesting through *values*: unlike keys, values are not hashed at | ||
| # construction time, so hashing recurses the full depth. | ||
| # See https://github.com/python/cpython/issues/154318. | ||
| fd = frozendict({0: 0}) | ||
| for _ in range(500_000): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is better to use |
||
| fd = frozendict({0: fd}) | ||
| with support.infinite_recursion(): | ||
| with self.assertRaises(RecursionError): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, assert regex |
||
| hash(fd) | ||
|
|
||
|
Comment on lines
+1922
to
+1933
|
||
| @support.cpython_only | ||
| def test_hash_cpython(self): | ||
| # Check that hash(frozendict) implementation is: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,16 @@ def check_one_exact(t, e32, e64): | |
| check_one_exact((0.5, (), (-2, 3, (4, 6))), 714642271, | ||
| -1845940830829704396) | ||
|
|
||
| def test_hash_deeply_nested(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments apply here as well. |
||
| # This should raise a RecursionError and not crash. | ||
| # See https://github.com/python/cpython/issues/154318. | ||
| t = () | ||
| for _ in range(500_000): | ||
| t = (t,) | ||
| with support.infinite_recursion(): | ||
| with self.assertRaises(RecursionError): | ||
| hash(t) | ||
|
|
||
|
Comment on lines
+122
to
+131
|
||
| # Various tests for hashing of tuples to check that we get few collisions. | ||
| # Does something only if RUN_ALL_HASH_TESTS is true. | ||
| # | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix a crash when hashing a deeply nested :class:`tuple` or :class:`frozendict`. | ||
| A :exc:`RecursionError` is now raised instead of overflowing the C stack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests with recursions must be properly guarded with skips.