gh-144984: Fix crash in ExternalEntityParserCreate() error paths#144992
Open
raminfp wants to merge 1 commit intopython:mainfrom
Open
gh-144984: Fix crash in ExternalEntityParserCreate() error paths#144992raminfp wants to merge 1 commit intopython:mainfrom
raminfp wants to merge 1 commit intopython:mainfrom
Conversation
When ExternalEntityParserCreate() hits an error path (allocation failure), Py_DECREF(new_parser) triggers xmlparse_dealloc() on a partially-initialized object: 1. handlers is NULL, so clear_handlers dereferences NULL (SEGV). 2. Py_CLEAR(parent) in dealloc already decrements the parent's refcount, so the explicit Py_DECREF(self) is a double-decrement. Fix by adding a NULL guard in clear_handlers and setting parent to NULL before Py_DECREF(new_parser) in each error path so that dealloc does not over-decrement the parent's refcount.
picnixz
reviewed
Feb 19, 2026
Comment on lines
+839
to
+842
| _testcapi = import_helper.import_module('_testcapi') | ||
| parser = expat.ParserCreate() | ||
| parser.buffer_text = True | ||
| rc_before = sys.getrefcount(parser) |
Member
There was a problem hiding this comment.
I suspect we'll need _testcapi later for this class (if we find other bugs) so add a setupClass method that binds cls.testcapi so that it can be called inthe methods.
Comment on lines
+845
to
+849
| try: | ||
| parser.ExternalEntityParserCreate(None) | ||
| except MemoryError: | ||
| pass | ||
| finally: |
Member
There was a problem hiding this comment.
Explcitily catch the MemoryError.
| if (self->buffer != NULL) { | ||
| new_parser->buffer = PyMem_Malloc(new_parser->buffer_size); | ||
| if (new_parser->buffer == NULL) { | ||
| new_parser->parent = NULL; |
Member
There was a problem hiding this comment.
Actually the issue isn't here, it's the decref on self. Instead, let's do new_parser->parent = Py_NewRef(self) and let the new parser's deallocator be responsible for decrefing this reference instead of us doing the Py_DECREF(self)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix crash when
ExternalEntityParserCreate()hits an error path(allocation failure).
Py_DECREF(new_parser)callsxmlparse_dealloc()on a partially-initialized object where
handlersis NULL, causing aNULL pointer dereference in
clear_handlers. Additionally,Py_CLEAR(parent)in dealloc already decrements the parent's refcount,making the subsequent
Py_DECREF(self)a double-decrement.clear_handlersparent = NULLbeforePy_DECREF(new_parser)in each error path