Skip to content

Commit 7ada36c

Browse files
committed
Keep in_callback set while a handler's result is released
The handler setter only installs the no-op handler in place of NULL while in_callback is set, but the trampolines reset it before releasing the handler's result. A destructor run at that point could still set the default handler to None, removing it while Expat was passing a long token to it in pieces. Also test unsetting DefaultHandlerExpand from within a callback, and reword the NEWS entry and a comment.
1 parent 2d0f5c6 commit 7ada36c

3 files changed

Lines changed: 64 additions & 14 deletions

File tree

‎Lib/test/test_pyexpat.py‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,17 +861,59 @@ def test_DefaultHandler(self):
861861
def test_DefaultHandlerExpand(self):
862862
self.check('DefaultHandlerExpand')
863863

864+
def check_unset_in_callback(self, name):
865+
parser = expat.ParserCreate()
866+
calls = []
867+
def handler(text):
868+
calls.append(text)
869+
if text.startswith('<!--'):
870+
setattr(parser, name, None)
871+
setattr(parser, name, handler)
872+
parser.Parse(self.xml, True)
873+
self.assertTrue(calls[-1].startswith('<!--'), calls)
874+
864875
def test_unset_DefaultHandler_in_callback(self):
876+
self.check_unset_in_callback('DefaultHandler')
877+
878+
def test_unset_DefaultHandlerExpand_in_callback(self):
879+
self.check_unset_in_callback('DefaultHandlerExpand')
880+
881+
def check_unset_in_result_destructor(self, name):
882+
# The handler's result is released after the handler has returned,
883+
# but while Expat is still passing the comment to the default handler.
865884
parser = expat.ParserCreate()
885+
class Result:
886+
def __del__(self):
887+
setattr(parser, name, None)
866888
calls = []
867889
def handler(text):
868890
calls.append(text)
869891
if text.startswith('<!--'):
870-
parser.DefaultHandler = None
871-
parser.DefaultHandler = handler
892+
return Result()
893+
setattr(parser, name, handler)
872894
parser.Parse(self.xml, True)
873895
self.assertTrue(calls[-1].startswith('<!--'), calls)
874896

897+
def test_unset_DefaultHandler_in_result_destructor(self):
898+
self.check_unset_in_result_destructor('DefaultHandler')
899+
900+
def test_unset_DefaultHandlerExpand_in_result_destructor(self):
901+
self.check_unset_in_result_destructor('DefaultHandlerExpand')
902+
903+
def test_unset_DefaultHandler_in_CharacterDataHandler_result(self):
904+
# With buffer_text, the CharacterDataHandler is called, and its
905+
# result released, just before the default handler is called.
906+
parser = expat.ParserCreate()
907+
class Result:
908+
def __del__(self):
909+
parser.DefaultHandler = None
910+
parser.buffer_text = True
911+
parser.CharacterDataHandler = lambda text: Result()
912+
parser.DefaultHandler = lambda text: None
913+
xml = ("<?xml version='1.0' encoding='iso8859'?><s>a<!--%s--></s>"
914+
% ('a' * 2000)).encode('iso8859')
915+
parser.Parse(xml, True)
916+
875917
def test_CharacterDataHandler_with_buffer_text(self):
876918
# With buffer_text, buffered character data is passed to the
877919
# CharacterDataHandler just before the default handler is called;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Fix a crash in :mod:`xml.parsers.expat` when the
2-
:meth:`~xml.parsers.expat.xmlparser.DefaultHandler` or
3-
:meth:`~xml.parsers.expat.xmlparser.DefaultHandlerExpand` callback raises an
4-
exception or unsets the handler while a token longer than 1024 bytes, such as
5-
a comment, is being reported from a document that is not encoded in UTF-8.
1+
Fix a crash in :mod:`xml.parsers.expat` when a handler raises an exception,
2+
or the :meth:`~xml.parsers.expat.xmlparser.DefaultHandler` or
3+
:meth:`~xml.parsers.expat.xmlparser.DefaultHandlerExpand` handler is unset,
4+
while a token longer than 1024 bytes, such as a comment, is being reported to
5+
the default handler from a document that is not encoded in UTF-8.

‎Modules/pyexpat.c‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,18 @@ call_character_handler(xmlparseobject *self, const XML_Char *buffer, int len)
385385
temp = call_with_frame("CharacterData", __LINE__,
386386
self->handlers[CharacterData], args, self);
387387
/* temp is an owned reference again, or NULL */
388-
self->in_callback = 0;
389388
Py_DECREF(args);
390389
if (temp == NULL) {
390+
self->in_callback = 0;
391391
flag_error(self);
392392
XML_SetCharacterDataHandler(self->itself,
393393
noop_character_data_handler);
394394
return -1;
395395
}
396+
/* Releasing the result can run arbitrary code, which must still be
397+
treated as being within the callback by the handler setter. */
396398
Py_DECREF(temp);
399+
self->in_callback = 0;
397400
return 0;
398401
}
399402

@@ -582,14 +585,18 @@ my_ ## NAME ## Handler PARAMS { \
582585
PyObject *rv = call_with_frame( \
583586
#NAME, __LINE__, \
584587
self->handlers[NAME], args, self); \
585-
self->in_callback = 0; \
586588
Py_DECREF(args); \
587589
if (rv == NULL) { \
590+
self->in_callback = 0; \
588591
flag_error(self); \
589592
return RETURN_VARIABLE; \
590593
} \
591594
CONVERSION \
595+
/* Releasing the result can run arbitrary code, \
596+
which must still be treated as being within \
597+
the callback by the handler setter. */ \
592598
Py_DECREF(rv); \
599+
self->in_callback = 0; \
593600
if (PyErr_Occurred()) { \
594601
invalid_expat_handler_rv(#NAME); \
595602
} \
@@ -2797,11 +2804,12 @@ clear_handlers(xmlparseobject *self, int initial)
27972804
Py_CLEAR(self->handlers[i]);
27982805
xmlhandler c_handler = NULL;
27992806
if (i == Default || i == DefaultHandlerExpand) {
2800-
/* Like the character data handler, the default handler
2801-
can't be removed from within a callback: for a document
2802-
that is not UTF-8, Expat passes a long piece of text to
2803-
it in several calls and re-reads the handler before each
2804-
one (reportDefault() in xmlparse.c). */
2807+
/* This may be called from within a callback, where the
2808+
default handler can't be removed: for a document that is
2809+
not UTF-8, Expat passes a long piece of text to it in
2810+
several calls and re-reads the handler before each one
2811+
(reportDefault() in xmlparse.c). The no-op handler is
2812+
harmless otherwise. */
28052813
c_handler = (xmlhandler)noop_character_data_handler;
28062814
}
28072815
CALL_XML_HANDLER_SETTER(&handler_info[i], self->itself, c_handler);

0 commit comments

Comments
 (0)