|
| 1 | +From f4eb9ab014545b521fb261b80adfa6d138e7e092 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Seth Michael Larson <seth@python.org> |
| 3 | +Date: Wed, 3 Dec 2025 01:16:37 -0600 |
| 4 | +Subject: [PATCH 1/4] gh-142145: Remove quadratic behavior in node ID cache |
| 5 | + clearing (GH-142146) |
| 6 | + |
| 7 | +* Remove quadratic behavior in node ID cache clearing |
| 8 | + |
| 9 | +Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> |
| 10 | + |
| 11 | +* Add news fragment |
| 12 | + |
| 13 | +--------- |
| 14 | +(cherry picked from commit 08d8e18ad81cd45bc4a27d6da478b51ea49486e4) |
| 15 | + |
| 16 | +Co-authored-by: Seth Michael Larson <seth@python.org> |
| 17 | +Co-authored-by: Jacob Walls <38668450+jacobtylerwalls@users.noreply.github.com> |
| 18 | + |
| 19 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 20 | +Upstream-reference: https://github.com/python/cpython/pull/142213.patch |
| 21 | +--- |
| 22 | + Lib/test/test_minidom.py | 33 ++++++++++++++++++- |
| 23 | + Lib/xml/dom/minidom.py | 11 ++----- |
| 24 | + ...-12-01-09-36-45.gh-issue-142145.tcAUhg.rst | 6 ++++ |
| 25 | + 3 files changed, 41 insertions(+), 9 deletions(-) |
| 26 | + create mode 100644 Misc/NEWS.d/next/Security/2025-12-01-09-36-45.gh-issue-142145.tcAUhg.rst |
| 27 | + |
| 28 | +diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py |
| 29 | +index 9762025..9f7f5b2 100644 |
| 30 | +--- a/Lib/test/test_minidom.py |
| 31 | ++++ b/Lib/test/test_minidom.py |
| 32 | +@@ -2,6 +2,7 @@ |
| 33 | + |
| 34 | + import copy |
| 35 | + import pickle |
| 36 | ++import time |
| 37 | + import io |
| 38 | + from test import support |
| 39 | + import unittest |
| 40 | +@@ -9,7 +10,7 @@ import unittest |
| 41 | + import pyexpat |
| 42 | + import xml.dom.minidom |
| 43 | + |
| 44 | +-from xml.dom.minidom import parse, Node, Document, parseString |
| 45 | ++from xml.dom.minidom import parse, Attr, Node, Document, Element, parseString |
| 46 | + from xml.dom.minidom import getDOMImplementation |
| 47 | + from xml.parsers.expat import ExpatError |
| 48 | + |
| 49 | +@@ -163,6 +164,36 @@ class MinidomTest(unittest.TestCase): |
| 50 | + self.confirm(dom.documentElement.childNodes[-1].data == "Hello") |
| 51 | + dom.unlink() |
| 52 | + |
| 53 | ++ @support.requires_resource('cpu') |
| 54 | ++ def testAppendChildNoQuadraticComplexity(self): |
| 55 | ++ impl = getDOMImplementation() |
| 56 | ++ |
| 57 | ++ newdoc = impl.createDocument(None, "some_tag", None) |
| 58 | ++ top_element = newdoc.documentElement |
| 59 | ++ children = [newdoc.createElement(f"child-{i}") for i in range(1, 2 ** 15 + 1)] |
| 60 | ++ element = top_element |
| 61 | ++ |
| 62 | ++ start = time.monotonic() |
| 63 | ++ for child in children: |
| 64 | ++ element.appendChild(child) |
| 65 | ++ element = child |
| 66 | ++ end = time.monotonic() |
| 67 | ++ |
| 68 | ++ # This example used to take at least 30 seconds. |
| 69 | ++ # Conservative assertion due to the wide variety of systems and |
| 70 | ++ # build configs timing based tests wind up run under. |
| 71 | ++ # A --with-address-sanitizer --with-pydebug build on a rpi5 still |
| 72 | ++ # completes this loop in <0.5 seconds. |
| 73 | ++ self.assertLess(end - start, 4) |
| 74 | ++ |
| 75 | ++ def testSetAttributeNodeWithoutOwnerDocument(self): |
| 76 | ++ # regression test for gh-142754 |
| 77 | ++ elem = Element("test") |
| 78 | ++ attr = Attr("id") |
| 79 | ++ attr.value = "test-id" |
| 80 | ++ elem.setAttributeNode(attr) |
| 81 | ++ self.assertEqual(elem.getAttribute("id"), "test-id") |
| 82 | ++ |
| 83 | + def testAppendChildFragment(self): |
| 84 | + dom, orig, c1, c2, c3, frag = self._create_fragment_test_nodes() |
| 85 | + dom.documentElement.appendChild(frag) |
| 86 | +diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py |
| 87 | +index d09ef5e..e4e8b42 100644 |
| 88 | +--- a/Lib/xml/dom/minidom.py |
| 89 | ++++ b/Lib/xml/dom/minidom.py |
| 90 | +@@ -292,13 +292,6 @@ def _append_child(self, node): |
| 91 | + childNodes.append(node) |
| 92 | + node.parentNode = self |
| 93 | + |
| 94 | +-def _in_document(node): |
| 95 | +- # return True iff node is part of a document tree |
| 96 | +- while node is not None: |
| 97 | +- if node.nodeType == Node.DOCUMENT_NODE: |
| 98 | +- return True |
| 99 | +- node = node.parentNode |
| 100 | +- return False |
| 101 | + |
| 102 | + def _write_data(writer, data): |
| 103 | + "Writes datachars to writer." |
| 104 | +@@ -355,6 +348,7 @@ class Attr(Node): |
| 105 | + def __init__(self, qName, namespaceURI=EMPTY_NAMESPACE, localName=None, |
| 106 | + prefix=None): |
| 107 | + self.ownerElement = None |
| 108 | ++ self.ownerDocument = None |
| 109 | + self._name = qName |
| 110 | + self.namespaceURI = namespaceURI |
| 111 | + self._prefix = prefix |
| 112 | +@@ -678,6 +672,7 @@ class Element(Node): |
| 113 | + |
| 114 | + def __init__(self, tagName, namespaceURI=EMPTY_NAMESPACE, prefix=None, |
| 115 | + localName=None): |
| 116 | ++ self.ownerDocument = None |
| 117 | + self.parentNode = None |
| 118 | + self.tagName = self.nodeName = tagName |
| 119 | + self.prefix = prefix |
| 120 | +@@ -1537,7 +1532,7 @@ def _clear_id_cache(node): |
| 121 | + if node.nodeType == Node.DOCUMENT_NODE: |
| 122 | + node._id_cache.clear() |
| 123 | + node._id_search_stack = None |
| 124 | +- elif _in_document(node): |
| 125 | ++ elif node.ownerDocument: |
| 126 | + node.ownerDocument._id_cache.clear() |
| 127 | + node.ownerDocument._id_search_stack= None |
| 128 | + |
| 129 | +diff --git a/Misc/NEWS.d/next/Security/2025-12-01-09-36-45.gh-issue-142145.tcAUhg.rst b/Misc/NEWS.d/next/Security/2025-12-01-09-36-45.gh-issue-142145.tcAUhg.rst |
| 130 | +new file mode 100644 |
| 131 | +index 0000000..05c7df3 |
| 132 | +--- /dev/null |
| 133 | ++++ b/Misc/NEWS.d/next/Security/2025-12-01-09-36-45.gh-issue-142145.tcAUhg.rst |
| 134 | +@@ -0,0 +1,6 @@ |
| 135 | ++Remove quadratic behavior in ``xml.minidom`` node ID cache clearing. In order |
| 136 | ++to do this without breaking existing users, we also add the *ownerDocument* |
| 137 | ++attribute to :mod:`xml.dom.minidom` elements and attributes created by directly |
| 138 | ++instantiating the ``Element`` or ``Attr`` class. Note that this way of creating |
| 139 | ++nodes is not supported; creator functions like |
| 140 | ++:py:meth:`xml.dom.Document.documentElement` should be used instead. |
| 141 | +-- |
| 142 | +2.45.4 |
| 143 | + |
0 commit comments