From 02a925100aa17c96ca33d28ae2a407e4ae6e7b1f Mon Sep 17 00:00:00 2001 From: lul-cas Date: Wed, 23 Sep 2026 20:29:08 -0300 Subject: [PATCH 1/4] gh-158032: Keep the element tag alive during Element.iter comparison --- Lib/test/test_xml_etree_c.py | 29 +++++++++++++++++++++++++++++ Modules/_elementtree.c | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index deb2bd8d572581a..cf616f300670a3c 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -161,6 +161,35 @@ def test_xmlpullparser_leaks(self): del parser support.gc_collect() + def test_iter_tag_reentrant_compare(self): + destroyed = [] + compared = [] + + class Tag: + def __del__(self): + destroyed.append(True) + + def __eq__(self, other): + child.tag = "replaced" + return NotImplemented + + __hash__ = object.__hash__ + + class Sought: + def __eq__(self, other): + if isinstance(other, Tag): + compared.append(not destroyed) + return NotImplemented + + __hash__ = object.__hash__ + + root = cET.Element("root") + child = cET.SubElement(root, "x") + child.tag = Tag() + self.assertEqual(list(root.iter(Sought())), []) + self.assertEqual(compared, [True]) + self.assertEqual(child.tag, "replaced") + def test_dict_disappearing_during_get_item(self): # test fix for seg fault reported in issue 27946 class X: diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 36115e61c2c2152..ee8617931f0b2ed 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -2381,7 +2381,9 @@ elementiter_next(PyObject *op) if (it->sought_tag == Py_None) return (PyObject *)elem; - rc = PyObject_RichCompareBool(elem->tag, it->sought_tag, Py_EQ); + PyObject *tag = Py_NewRef(elem->tag); + rc = PyObject_RichCompareBool(tag, it->sought_tag, Py_EQ); + Py_DECREF(tag); if (rc > 0) return (PyObject *)elem; From 29e03bb4caa39d23f1cf1acc04ce6d72a5a65336 Mon Sep 17 00:00:00 2001 From: lul-cas Date: Wed, 23 Sep 2026 21:47:14 -0300 Subject: [PATCH 2/4] gh-158032: Add the NEWS entry for the Element.iter fix --- .../next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst diff --git a/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst new file mode 100644 index 000000000000000..368ce48df6614e0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst @@ -0,0 +1,2 @@ +Fix a use-after-free in :meth:`xml.etree.ElementTree.Element.iter` when a +tag's equality comparison replaces that tag. From 44c2f6b2b1c97b45c34037f8b89f7e5ac6b77d2c Mon Sep 17 00:00:00 2001 From: lul-cas Date: Thu, 12 Nov 2026 15:29:43 -0300 Subject: [PATCH 3/4] gh-158032: Simplify the Element.iter test and mention the crash in NEWS --- Lib/test/test_xml_etree_c.py | 9 --------- .../2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst | 4 ++-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index cf616f300670a3c..121b3670fefa2c8 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -162,13 +162,7 @@ def test_xmlpullparser_leaks(self): support.gc_collect() def test_iter_tag_reentrant_compare(self): - destroyed = [] - compared = [] - class Tag: - def __del__(self): - destroyed.append(True) - def __eq__(self, other): child.tag = "replaced" return NotImplemented @@ -177,8 +171,6 @@ def __eq__(self, other): class Sought: def __eq__(self, other): - if isinstance(other, Tag): - compared.append(not destroyed) return NotImplemented __hash__ = object.__hash__ @@ -187,7 +179,6 @@ def __eq__(self, other): child = cET.SubElement(root, "x") child.tag = Tag() self.assertEqual(list(root.iter(Sought())), []) - self.assertEqual(compared, [True]) self.assertEqual(child.tag, "replaced") def test_dict_disappearing_during_get_item(self): diff --git a/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst index 368ce48df6614e0..0e0a565519fa8e6 100644 --- a/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst +++ b/Misc/NEWS.d/next/Library/2026-09-23-21-15-00.gh-issue-158032.k4nT9p.rst @@ -1,2 +1,2 @@ -Fix a use-after-free in :meth:`xml.etree.ElementTree.Element.iter` when a -tag's equality comparison replaces that tag. +Fix a crash in :meth:`xml.etree.ElementTree.Element.iter` when a tag's +equality comparison replaces that tag. From e96db70904369989adc9ef5617900a4257e1ff48 Mon Sep 17 00:00:00 2001 From: lul-cas Date: Thu, 12 Nov 2026 16:55:28 -0300 Subject: [PATCH 4/4] gh-158032: Restore the Element.iter test that detects the crash --- Lib/test/test_xml_etree_c.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 121b3670fefa2c8..cf616f300670a3c 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -162,7 +162,13 @@ def test_xmlpullparser_leaks(self): support.gc_collect() def test_iter_tag_reentrant_compare(self): + destroyed = [] + compared = [] + class Tag: + def __del__(self): + destroyed.append(True) + def __eq__(self, other): child.tag = "replaced" return NotImplemented @@ -171,6 +177,8 @@ def __eq__(self, other): class Sought: def __eq__(self, other): + if isinstance(other, Tag): + compared.append(not destroyed) return NotImplemented __hash__ = object.__hash__ @@ -179,6 +187,7 @@ def __eq__(self, other): child = cET.SubElement(root, "x") child.tag = Tag() self.assertEqual(list(root.iter(Sought())), []) + self.assertEqual(compared, [True]) self.assertEqual(child.tag, "replaced") def test_dict_disappearing_during_get_item(self):