Skip to content

gh-145244: Fix use-after-free on borrowed dict key in json encoder#145245

Open
raminfp wants to merge 1 commit intopython:mainfrom
raminfp:fix-gh-145244-json-uaf-key
Open

gh-145244: Fix use-after-free on borrowed dict key in json encoder#145245
raminfp wants to merge 1 commit intopython:mainfrom
raminfp:fix-gh-145244-json-uaf-key

Conversation

@raminfp
Copy link
Contributor

@raminfp raminfp commented Feb 26, 2026

Fix use-after-free in encoder_encode_key_value() where borrowed references to dict keys from PyDict_Next() can become dangling pointers if the default callback mutates the dictionary during encoding.

In encoder_encode_key_value(), key is a borrowed reference from
PyDict_Next(). If the default callback mutates or clears the dict,
key becomes a dangling pointer. The error path then calls
_PyErr_FormatNote("%R", key) on freed memory.

Fix by holding strong references to key and value unconditionally
during encoding, not just in the free-threading build.
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.

1 participant