Skip to content

Commit 58534a3

Browse files
committed
gh-146174: Prevent re-entrant Parse() calls in pyexpat
Add a check in Parse() to prevent calls when in_callback is true, as this violates expat's requirements and can cause crashes. Now raises RuntimeError with a clear message. Add tests to verify the fix and ensure normal parsing still works.
1 parent d5b2681 commit 58534a3

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Lib/test/test_pyexpat.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,32 @@ def test_parse_again(self):
276276
self.assertEqual(expat.ErrorString(cm.exception.code),
277277
expat.errors.XML_ERROR_FINISHED)
278278

279+
def test_reentrant_parse_crash(self):
280+
from xml.parsers import expat
281+
p = expat.ParserCreate(encoding="utf-16")
282+
283+
def start(name, attrs):
284+
def handler(data):
285+
p.Parse(data, 0)
286+
p.CharacterDataHandler = handler
287+
288+
p.StartElementHandler = start
289+
data = b"\xff\xfe<\x00a\x00>\x00x\x00"
290+
with self.assertRaises(RuntimeError) as cm:
291+
for i in range(len(data)):
292+
p.Parse(data[i:i+1], i == len(data) - 1)
293+
self.assertEqual(str(cm.exception),
294+
"cannot call Parse() from within a handler")
295+
296+
def test_parse_normal(self):
297+
from xml.parsers import expat
298+
p = expat.ParserCreate()
299+
data = "<root><child/></root>".encode('utf-8')
300+
try:
301+
p.Parse(data, 1)
302+
except RuntimeError:
303+
self.fail("Parse() raised RuntimeError during normal operation")
304+
279305
class NamespaceSeparatorTest(unittest.TestCase):
280306
def test_legal(self):
281307
# Tests that make sure we get errors when the namespace_separator value
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. gh-issue: 146174
2+
3+
.. section: Library
4+
5+
Prevent re-entrant calls to
6+
:meth:`~xml.parsers.expat.xmlparser.Parse` from
7+
within expat handlers, which could cause a crash. Now raises
8+
:exc:`RuntimeError` when such a call is attempted.

Modules/pyexpat.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,13 @@ pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyTypeObject *cls,
857857
PyObject *data, int isfinal)
858858
/*[clinic end generated code: output=8faffe07fe1f862a input=053e0f047e55c05a]*/
859859
{
860+
861+
if (self->in_callback) {
862+
PyErr_SetString(PyExc_RuntimeError,
863+
"cannot call Parse() from within a handler");
864+
return NULL;
865+
}
866+
860867
const char *s;
861868
Py_ssize_t slen;
862869
Py_buffer view;

0 commit comments

Comments
 (0)