Skip to content

gh-161767: Fix crash in PyDict_AsFrozenDictAndClear on embedded values - #162139

Open
sobolevn wants to merge 1 commit into
python:mainfrom
sobolevn:issue-161767
Open

sobolevn wants to merge 1 commit into
python:mainfrom
sobolevn:issue-161767

Conversation

@sobolevn

@sobolevn sobolevn commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

My alternative to #161768

I've also added more tests for dicts with non-str keys. Because they are a bit different now. str keys take less memory. It is worth testing.

self.assertIs(type(d), dict)
self.assertEqual(d, {})

d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'})

Copy link
Copy Markdown
Member

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?

with self.subTest(klass=klass):
instance = klass()
instance.key = 'value'
with self.assertRaisesRegex(ValueError, 'embedded values'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaisesRegex(ValueError, 'embedded values'):
with self.assertRaisesRegex(ValueError, 'dict with embedded values'):

with self.assertRaises(SystemError):
as_frozendict(wrong_input)

# gh-161767: check embedded values

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# gh-161767: check embedded values
# gh-161767: check type with inline values and dict with embedded values

Comment thread Objects/dictobject.c
}

PyDictObject *ma = (PyDictObject*)dict;
if (ma->ma_values && ma->ma_values->embedded) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may safer to move the check inside transfer_keys_and_values_lock_held(), so it's protected by Py_BEGIN_CRITICAL_SECTION(dict). So transfer_keys_and_values_lock_held() would return an error.

_PyDict_DetachFromObject() can also modify mp->ma_values, and mp->ma_values->embedded == 1 becomes mp->ma_values->embedded == 0.

Also, on MemoryError, PyObject_ClearManagedDict() sets dict->ma_values = NULL;.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants