]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-139210: Fix use-after-free in xml.etree.ElementTree.iterparse() (GH-139211...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 30 Sep 2025 18:14:44 +0000 (20:14 +0200)
committerGitHub <noreply@github.com>
Tue, 30 Sep 2025 18:14:44 +0000 (18:14 +0000)
(cherry picked from commit c86eb4d3ac5984efc1ea920ba643e3c4f02fdee8)

Co-authored-by: Ken Jin <kenjin@python.org>
Lib/test/test_xml_etree.py
Misc/NEWS.d/next/Core_and_Builtins/2025-09-21-15-58-57.gh-issue-139210.HGbMvz.rst [new file with mode: 0644]
Modules/_elementtree.c

index 12efa006cd509deb553d53039af0edefb4f67305..78598b35dae050a05bfb6c95d42d498533e8ced7 100644 (file)
@@ -1750,6 +1750,8 @@ class XMLPullParserTest(unittest.TestCase):
     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 '
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-21-15-58-57.gh-issue-139210.HGbMvz.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-21-15-58-57.gh-issue-139210.HGbMvz.rst
new file mode 100644 (file)
index 0000000..1227b29
--- /dev/null
@@ -0,0 +1 @@
+Fix use-after-free when reporting unknown event in :func:`xml.etree.ElementTree.iterparse`. Patch by Ken Jin.
index 3926ef3ef835e18d959d07304b58b31cea78bd2b..020b7454add2245a729b70127d413f6e64026376 100644 (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;
         }
     }