Skip to content

Commit 8f8884b

Browse files
Add test for heap overflow in expat parser's CharacterDataHandler
1 parent 6138a78 commit 8f8884b

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Lib/test/test_pyexpat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,20 @@ def test_change_size_2(self):
712712
parser.Parse(xml2, True)
713713
self.assertEqual(self.n, 4)
714714

715+
@support.requires_resource('cpu')
716+
@support.requires_resource('walltime')
717+
def test_heap_overflow(self):
718+
# See https://github.com/python/cpython/issues/148441
719+
parser = expat.ParserCreate()
720+
parser.buffer_text = True
721+
parser.buffer_size = 2**31 - 1 # INT_MAX
722+
def handler(text):
723+
pass
724+
N = 2049 * (1 << 20) - 3 # 2,148,532,221 bytes of character data
725+
parser.CharacterDataHandler = handler
726+
xml_data = b"<r>" + b"A" * N + b"</r>"
727+
self.assertEqual(parser.Parse(xml_data, True), 1)
728+
715729
class ElementDeclHandlerTest(unittest.TestCase):
716730
def test_trigger_leak(self):
717731
# Unfixed, this test would leak the memory of the so-called

0 commit comments

Comments
 (0)