Skip to content

Commit 88ff32a

Browse files
authored
Fix GH-14343: Memory leak in xml and dom (#14347)
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.
1 parent a05301e commit 88ff32a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PHP NEWS
1010
- Curl:
1111
. Fixed bug GH-14307 (Test curl_basic_024 fails with curl 8.8.0). (nielsdos)
1212

13+
- DOM:
14+
. Fixed bug GH-14343 (Memory leak in xml and dom). (nielsdos)
15+
1316
- Opcache:
1417
. Fixed bug GH-14267 (opcache.jit=off does not allow enabling JIT at runtime).
1518
(ilutov)

ext/dom/document.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,13 @@ PHP_METHOD(DOMDocument, importNode)
848848
if (nsptr == NULL) {
849849
int errorcode;
850850
nsptr = dom_get_ns(root, (char *) nodep->ns->href, &errorcode, (char *) nodep->ns->prefix);
851+
852+
/* If there is no root, the namespace cannot be attached to it, so we have to attach it to the old list. */
853+
if (nsptr != NULL && root == NULL) {
854+
dom_set_old_ns(nodep->doc, nsptr);
855+
}
851856
}
852-
xmlSetNs(retnodep, nsptr);
857+
retnodep->ns = nsptr;
853858
}
854859
}
855860

ext/dom/tests/gh14343.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-14343 (Memory leak in xml and dom)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$aDOM = new DOMDocument();
8+
$fromdom = new DOMDocument();
9+
$fromdom->loadXML('<data xmlns:ai="http://test.org" ai:attr="namespaced" />');
10+
$attr= $fromdom->firstChild->attributes->item(0);
11+
$att = $aDOM->importNode($attr);
12+
echo $aDOM->saveXML($att);
13+
?>
14+
--EXPECT--
15+
ai:attr="namespaced"

0 commit comments

Comments
 (0)