When ExternalEntityParserCreate() hits an error path (allocation failure), Py_DECREF(new_parser) is called on a partially-initialized object. This has two problems:
new_parser->handlers is NULL (set to 0 at line 1094), so xmlparse_dealloc to clear_handlers dereferences NULL to SEGV
new_parser->parent holds a strong ref to self, so Py_DECREF(new_parser) already decrements self via Py_CLEAR(parent) in dealloc. The explicit Py_DECREF(self) on the next line is a double decrement.
All three error paths in the function have the same issue (lines 1100-1101, 1106-1107, 1120-1121).
Activity
raminfp commented on Feb 19, 2026
The bug was introduced with the parent field (gh-139400 fix).
gh-144984: Fix crash in Expat's `ExternalEntityParserCreate` error pa…
pythongh-144984: Fix crash in Expat's `ExternalEntityParserCreate` er…
[3.14] gh-144984: Fix crash in Expat's `ExternalEntityParserCreate` e…
pythongh-144984: Skip test unper tracerefs
pythongh-144984: Skip test under tracerefs
encukou commented on Mar 20, 2026
The test added in GH-144992 fails in tracerefs mode, which needs memory allocation for its validation magic, and fails after testcapi.set_nomemory.
Example failure: https://buildbot.python.org/#/builders/1747/builds/263/steps/6/logs/stdio
I'll send a PR.
gh-144984: Skip test under tracerefs (GH-146218)
pythongh-144984: Skip test under tracerefs (pythonGH-146218)
[3.14] gh-144984: Skip test under tracerefs (GH-146218) (GH-146384)
[3.13] gh-144984: Fix crash in Expat's `ExternalEntityParserCreate` e…
gpshead commented on Apr 11, 2026
thanks, this appears to be fixed now!
pythongh-144984: Skip test under tracerefs (pythonGH-146218)
pythongh-144984: Fix crash in Expat's `ExternalEntityParserCreate` er…
pythongh-144984: Skip test under tracerefs (pythonGH-146218)