[3.13] gh-139210: Fix use-after-free in xml.etree.ElementTree.iterparse() (GH-139211) (GH-139456)

(cherry picked from commit c86eb4d3ac)

Co-authored-by: Ken Jin <kenjin@python.org>
This commit is contained in:
Miss Islington (bot) 2025-09-30 20:14:44 +02:00 committed by GitHub
parent 0ef302e019
commit f48128b6b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 4 additions and 1 deletions

View file

@ -1750,6 +1750,8 @@ def __next__(self):
def test_unknown_event(self):
with self.assertRaises(ValueError):
ET.XMLPullParser(events=('start', 'end', 'bogus'))
with self.assertRaisesRegex(ValueError, "unknown event 'bogus'"):
ET.XMLPullParser(events=(x.decode() for x in (b'start', b'end', b'bogus')))
@unittest.skipIf(pyexpat.version_info < (2, 6, 0),
f'Expat {pyexpat.version_info} does not '

View file

@ -0,0 +1 @@
Fix use-after-free when reporting unknown event in :func:`xml.etree.ElementTree.iterparse`. Patch by Ken Jin.

View file

@ -4180,8 +4180,8 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,
(XML_ProcessingInstructionHandler) expat_pi_handler
);
} else {
Py_DECREF(events_seq);
PyErr_Format(PyExc_ValueError, "unknown event '%s'", event_name);
Py_DECREF(events_seq);
return NULL;
}
}