Skip to content

gh-161060: Fix pyexpat crash when the default handler is cleared - #161061

Open
gpshead wants to merge 2 commits into
python:mainfrom
gpshead:scanfix-pyexpat-0sjb3wwc
Open

gpshead wants to merge 2 commits into
python:mainfrom
gpshead:scanfix-pyexpat-0sjb3wwc

Conversation

@gpshead

@gpshead gpshead commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Expat calls the default handler once per 1024-byte piece of a token that needs encoding conversion and re-reads the handler pointer between pieces, so pyexpat must not set it to NULL from inside a callback. When handlers are cleared after an error, or DefaultHandler/DefaultHandlerExpand is set to None from inside a callback, install the existing no-op handler instead, as has been done for the character data handler since gh-42389. The Python-level handlers are still cleared, so no Python code runs after an error, and the handler setters are called in the same order, so the internal-entity expansion setting ends up as before.

Fixes #161060

ANT-2026-0SJB3WWC

Expat calls the default handler once per 1024-byte piece of a token that
needs encoding conversion.  Install a no-op instead of NULL when the
handler is cleared after an error or unset from within a callback, as is
already done for the character data handler.
@gpshead
gpshead marked this pull request as ready for review November 1, 2026 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xml.parsers.expat crashes when DefaultHandler raises on a long non-UTF-8 token

1 participant