Repository navigation
gh-161767: Fix crash in PyDict_AsFrozenDictAndClear() with inline values #161768
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
0757d86
a3112a5
3fd3f1e
0600aa7
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,6 +635,18 @@ class DictSubtype(dict): ... | |
| self.assertIs(type(d), DictSubtype) | ||
| self.assertEqual(d, DictSubtype({})) | ||
|
|
||
| # instance dict that still has inline values | ||
| class A: ... | ||
| a = A() | ||
| a.x = 1 | ||
| f = as_frozendict(a.__dict__) | ||
| self.assertEqual(vars(a), {}) | ||
|
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 feels weird that this mutates instance's |
||
| a.x = 2 | ||
| self.assertEqual(f, frozendict(x=1)) | ||
| self.assertEqual(vars(a), {'x': 2}) | ||
| del a | ||
| self.assertEqual(f, frozendict(x=1)) | ||
|
|
||
| for wrong_input in (frozendict(), [], None): | ||
| with self.subTest(wrong_input=wrong_input): | ||
| with self.assertRaises(SystemError): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix a crash in :c:func:`PyDict_AsFrozenDictAndClear` when called on an | ||
| instance :attr:`~object.__dict__` that still uses inline values. |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8662,33 +8662,50 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | |||||||||
| return d; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| static void | ||||||||||
| static int | ||||||||||
| transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict) | ||||||||||
| { | ||||||||||
| PyDictObject *new = (PyDictObject *)res; | ||||||||||
| PyDictObject *old = (PyDictObject *)dict; | ||||||||||
| assert(can_modify_dict(old)); | ||||||||||
|
|
||||||||||
| // Fast path: do nothing on an empty dict: | ||||||||||
| if (old->ma_keys == Py_EMPTY_KEYS) { | ||||||||||
| return; | ||||||||||
| return 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Py_ssize_t used = old->ma_used; | ||||||||||
| PyDictKeysObject *keys = old->ma_keys; | ||||||||||
| PyDictValues *values = old->ma_values; | ||||||||||
|
|
||||||||||
| // Clear the old dict keys and values, but do not decref them: | ||||||||||
| clear_common(old); | ||||||||||
| set_keys(old, Py_EMPTY_KEYS); | ||||||||||
| set_values(old, NULL); | ||||||||||
| if (values && values->embedded) { | ||||||||||
| // values are inline in the object, copy them and clear the object's copy | ||||||||||
| PyDictValues *v = values; | ||||||||||
| values = copy_values(v); | ||||||||||
| if (!values) { | ||||||||||
| PyErr_NoMemory(); | ||||||||||
| return -1; | ||||||||||
| } | ||||||||||
| dictkeys_incref(keys); | ||||||||||
|
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. Question: why do we need the incref here? |
||||||||||
| clear_common(old); | ||||||||||
| for (Py_ssize_t i = 0; i < v->capacity; i++) { | ||||||||||
|
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. Maybe |
||||||||||
| FT_ATOMIC_STORE_PTR_RELEASE(v->values[i], NULL); | ||||||||||
| } | ||||||||||
| FT_ATOMIC_STORE_UINT8_RELEASE(v->size, 0); | ||||||||||
| } | ||||||||||
| else { | ||||||||||
| clear_common(old); | ||||||||||
| set_keys(old, Py_EMPTY_KEYS); | ||||||||||
| set_values(old, NULL); | ||||||||||
| } | ||||||||||
| ASSERT_CONSISTENT(old); | ||||||||||
|
|
||||||||||
| // Transfer keys and values from dict to frozendict: | ||||||||||
| // Only transfer keys and values to the new dict | ||||||||||
| // once the old dict is cleared | ||||||||||
| new->ma_used = used; | ||||||||||
|
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
|
||||||||||
| new->ma_keys = keys; | ||||||||||
| new->ma_values = values; | ||||||||||
| ASSERT_CONSISTENT(new); | ||||||||||
| return 0; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| PyObject * | ||||||||||
|
|
@@ -8704,9 +8721,14 @@ PyDict_AsFrozenDictAndClear(PyObject *dict) | |||||||||
| return NULL; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| int err; | ||||||||||
| Py_BEGIN_CRITICAL_SECTION(dict); | ||||||||||
| transfer_keys_and_values_lock_held(res, dict); | ||||||||||
| err = transfer_keys_and_values_lock_held(res, dict); | ||||||||||
| Py_END_CRITICAL_SECTION(); | ||||||||||
| if (err < 0) { | ||||||||||
| Py_DECREF(res); | ||||||||||
| return NULL; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| _PyObject_GC_TRACK(res); | ||||||||||
| assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1); | ||||||||||
|
|
||||||||||
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.
I would prefer to check also a just after as_frozendict() call: