Visitar URL original
gh-139210: Fix use-after-free in _elementtree_XMLParser__setevents_impl by Fidget-Spinner · Pull Request #139211 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250922050907/https://github.com/python/cpython/pull/139211
Skip to content

Conversation

Fidget-Spinner
Copy link
Member

@Fidget-Spinner Fidget-Spinner commented Sep 21, 2025 •

@Fidget-Spinner
Copy link
Member Author

Does anyone know how to add a test for this? I don't see how as the normal interpreter doesn't crash with this.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

The core change looks good. I would just add a test case using something like the original repro. We shouldn't need anything from gettext or fancy Unicode characters to reproduce this, as long as the iterable to events has a reference count of 1 and the string inside it is not interned/immortal.

I think this only crashes with the JIT because the JIT avoids reference count increases, right? On the normal build, the interpreter is seemingly holding an extra reference to the event name on its stack, which keeps it alive just long enough to be used in the exception. That step is probably optimized out when the JIT is enabled, so the Py_DECREF(events_seq) will actually deallocate the string.

@@ -0,0 +1 @@
Fix use after free when handling unicode characters in ``xml.etree.ElementTree.iterparse``. Patch by Ken Jin.
Copy link
Member

Choose a reason for hiding this comment

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

Some nitpicks:

Suggested change
Fix use after free when handling unicode characters in ``xml.etree.ElementTree.iterparse``. Patch by Ken Jin.
Fix use-after-free when handling unicode characters in :func:`xml.etree.ElementTree.iterparse`. Patch by Ken Jin.

This also isn't really specific to Unicode characters, but I don't mind keeping the mention.

@Fidget-Spinner
Copy link
Member Author

I think this only crashes with the JIT because the JIT avoids reference count increases, right? On the normal build, the interpreter is seemingly holding an extra reference to the event name on its stack, which keeps it alive just long enough to be used in the exception. That step is probably optimized out when the JIT is enabled, so the Py_DECREF(events_seq) will actually deallocate the string.

No ASAN triggers on the normal build as well it seems.

@ZeroIntensity
Copy link
Member

Yeah, that makes sense. The memory isn't freed on the default build (at least in the repro), because something else has an extra reference (likely the interpreter stack). It'd probably be possible to get a repro for the default build by constructing some iterator in C that the interpreter can't touch.

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