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"