Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,23 @@ def test_change_size_2(self):
parser.Parse(xml2, True)
self.assertEqual(self.n, 4)

class ElementDeclHandlerTest(unittest.TestCase):
def test_trigger_leak(self):
# Unfixed, this test would leak the memory of the so-called
# "content model" in function ``my_ElementDeclHandler`` of pyexpat.
# See https://github.com/python/cpython/issues/140593.
data = textwrap.dedent('''\
<!DOCTYPE quotations SYSTEM "quotations.dtd" [
<!ELEMENT root ANY>
]>
<root/>
''').encode('UTF-8')

parser = expat.ParserCreate()
parser.NotStandaloneHandler = lambda: 1.234 # arbitrary float
parser.ElementDeclHandler = lambda _1, _2: None
self.assertRaises(TypeError, parser.Parse, data, True)

class MalformedInputTest(unittest.TestCase):
def test1(self):
xml = b"\0\r\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`xml.parsers.expat`: Fix a memory leak that could affect users with
:meth:`~xml.parsers.expat.xmlparser.ElementDeclHandler` set to a custom
element declaration handler. Patch by Sebastian Pipping.
2 changes: 1 addition & 1 deletion Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ my_ElementDeclHandler(void *userData,
PyObject *modelobj, *nameobj;

if (PyErr_Occurred())
return;
goto finally;

if (flush_character_buffer(self) < 0)
goto finally;
Expand Down
Loading