Repository navigation
gh-161767: Fix crash in PyDict_AsFrozenDictAndClear on embedded values
#162139
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -635,11 +635,42 @@ class DictSubtype(dict): ... | |||||
| self.assertIs(type(d), DictSubtype) | ||||||
| self.assertEqual(d, DictSubtype({})) | ||||||
|
|
||||||
| # non str keys: | ||||||
| d = {1: 'a', 2: 'b', 3: 'c', None: 'f'} | ||||||
| f = as_frozendict(d) | ||||||
| self.assertIs(type(f), frozendict) | ||||||
| self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'})) | ||||||
| self.assertIs(type(d), dict) | ||||||
| self.assertEqual(d, {}) | ||||||
|
|
||||||
| d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'}) | ||||||
| f = as_frozendict(d) | ||||||
| self.assertIs(type(f), frozendict) | ||||||
| self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'})) | ||||||
| self.assertIs(type(d), DictSubtype) | ||||||
| self.assertEqual(d, DictSubtype({})) | ||||||
|
|
||||||
| # wrong inputs: | ||||||
| for wrong_input in (frozendict(), [], None): | ||||||
| with self.subTest(wrong_input=wrong_input): | ||||||
| with self.assertRaises(SystemError): | ||||||
| as_frozendict(wrong_input) | ||||||
|
|
||||||
| # gh-161767: check embedded values | ||||||
|
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.
Suggested change
|
||||||
| class Regular: ... | ||||||
|
|
||||||
| for klass in (Regular, _testcapi.ManagedDictType): | ||||||
| with self.subTest(klass=klass): | ||||||
| instance = klass() | ||||||
| instance.key = 'value' | ||||||
| with self.assertRaisesRegex(ValueError, 'embedded values'): | ||||||
|
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.
Suggested change
|
||||||
| as_frozendict(instance.__dict__) | ||||||
|
|
||||||
| self.assertEqual(instance.__dict__, {'key': 'value'}) | ||||||
| # Both of these actions were needed to cause a crash: | ||||||
| instance.other = 'new' | ||||||
| del instance # must not crash | ||||||
|
|
||||||
| def test_frozendict_check(self): | ||||||
| # Test PyFrozenDict_Check() | ||||||
| check = _testcapi.frozendict_check | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8700,6 +8700,13 @@ PyDict_AsFrozenDictAndClear(PyObject *dict) | |
| return NULL; | ||
| } | ||
|
|
||
| PyDictObject *ma = (PyDictObject*)dict; | ||
| if (ma->ma_values && ma->ma_values->embedded) { | ||
|
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 may safer to move the check inside
Also, on MemoryError, |
||
| PyErr_SetString(PyExc_ValueError, | ||
| "can't convert dict with embedded values"); | ||
| return NULL; | ||
| } | ||
|
|
||
| PyObject *res = frozendict_new_untracked(&PyFrozenDict_Type); | ||
| if (res == NULL) { | ||
| return NULL; | ||
|
|
||
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.
If you add this test, is it still useful to keep
d = DictSubtype(x=123)test above?