From 6fcf45b23a5f6fe8ca0efb5aaff9af06c22b9062 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 28 May 2024 20:09:26 +0200 Subject: [PATCH] Fix GH-14343: Memory leak in xml and dom If there is no root, the namespace cannot be attached to it, so we have to attach it to the old list. This isn't a problem in "new DOM" because namespaces are managed in a separate structure there. --- NEWS | 3 +++ ext/dom/document.c | 7 ++++++- ext/dom/tests/gh14343.phpt | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 ext/dom/tests/gh14343.phpt diff --git a/NEWS b/NEWS index 6cec84d3df660..ee6792716c6b1 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,9 @@ PHP NEWS - Curl: . Fixed bug GH-14307 (Test curl_basic_024 fails with curl 8.8.0). (nielsdos) +- DOM: + . Fixed bug GH-14343 (Memory leak in xml and dom). (nielsdos) + - Opcache: . Fixed bug GH-14267 (opcache.jit=off does not allow enabling JIT at runtime). (ilutov) diff --git a/ext/dom/document.c b/ext/dom/document.c index 8312d6c59399f..a2772479cfe8e 100644 --- a/ext/dom/document.c +++ b/ext/dom/document.c @@ -848,8 +848,13 @@ PHP_METHOD(DOMDocument, importNode) if (nsptr == NULL) { int errorcode; nsptr = dom_get_ns(root, (char *) nodep->ns->href, &errorcode, (char *) nodep->ns->prefix); + + /* If there is no root, the namespace cannot be attached to it, so we have to attach it to the old list. */ + if (nsptr != NULL && root == NULL) { + dom_set_old_ns(nodep->doc, nsptr); + } } - xmlSetNs(retnodep, nsptr); + retnodep->ns = nsptr; } } diff --git a/ext/dom/tests/gh14343.phpt b/ext/dom/tests/gh14343.phpt new file mode 100644 index 0000000000000..7cece29f4cd56 --- /dev/null +++ b/ext/dom/tests/gh14343.phpt @@ -0,0 +1,15 @@ +--TEST-- +GH-14343 (Memory leak in xml and dom) +--EXTENSIONS-- +dom +--FILE-- +loadXML(''); +$attr= $fromdom->firstChild->attributes->item(0); +$att = $aDOM->importNode($attr); +echo $aDOM->saveXML($att); +?> +--EXPECT-- + ai:attr="namespaced"