Skip to content

Commit d9329b1

Browse files
committed
Fix xinclude destruction of live attributes
Follow-up for GH-17847 but now for attributes. Closes GH-18100.
1 parent c531f3d commit d9329b1

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ PHP NEWS
1010

1111
- DOM:
1212
. Fix weird unpack behaviour in DOM. (nielsdos)
13+
. Fix xinclude destruction of live attributes. (nielsdos)
1314

1415
- GD:
1516
. Fixed bug GH-17984 (calls with arguments as array with references).

ext/dom/document.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,14 +1607,28 @@ static zend_always_inline xmlNodePtr php_dom_next_in_tree_order(const xmlNode *n
16071607
}
16081608
}
16091609

1610+
static void dom_xinclude_strip_references_for_attributes(xmlNodePtr basep)
1611+
{
1612+
for (xmlAttrPtr prop = basep->properties; prop; prop = prop->next) {
1613+
php_libxml_node_free_resource((xmlNodePtr) prop);
1614+
for (xmlNodePtr child = prop->children; child; child = child->next) {
1615+
php_libxml_node_free_resource(child);
1616+
}
1617+
}
1618+
}
1619+
16101620
static void dom_xinclude_strip_references(xmlNodePtr basep)
16111621
{
16121622
php_libxml_node_free_resource(basep);
1623+
dom_xinclude_strip_references_for_attributes(basep);
16131624

16141625
xmlNodePtr current = basep->children;
16151626

16161627
while (current) {
16171628
php_libxml_node_free_resource(current);
1629+
if (current->type == XML_ELEMENT_NODE) {
1630+
dom_xinclude_strip_references_for_attributes(current);
1631+
}
16181632
current = php_dom_next_in_tree_order(current, basep);
16191633
}
16201634
}

ext/dom/tests/gh17847.phpt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $doc->loadXML(<<<XML
1313
</xi:include>
1414
<xi:test xmlns:xi="http://www.w3.org/2001/XInclude">
1515
<xi:include href="thisisnonexistent">
16-
<p>garbage</p>
16+
<p attr="foo" attr2="bar">garbage</p>
1717
</xi:include>
1818
</xi:test>
1919
</root>
@@ -22,15 +22,22 @@ XML);
2222
$xpath = new DOMXPath($doc);
2323

2424
$garbage = [];
25-
foreach ($xpath->query('//p') as $entry)
25+
foreach ($xpath->query('//p') as $entry) {
2626
$garbage[] = $entry;
27+
foreach ($entry->attributes as $attr) {
28+
$garbage[] = $attr;
29+
foreach ($attr->childNodes as $child) {
30+
$garbage[] = $child;
31+
}
32+
}
33+
}
2734

2835
@$doc->xinclude();
2936

3037
var_dump($garbage);
3138
?>
3239
--EXPECT--
33-
array(3) {
40+
array(7) {
3441
[0]=>
3542
object(DOMElement)#3 (1) {
3643
["schemaTypeInfo"]=>
@@ -46,4 +53,24 @@ array(3) {
4653
["schemaTypeInfo"]=>
4754
NULL
4855
}
56+
[3]=>
57+
object(DOMAttr)#10 (2) {
58+
["specified"]=>
59+
bool(true)
60+
["schemaTypeInfo"]=>
61+
NULL
62+
}
63+
[4]=>
64+
object(DOMText)#13 (0) {
65+
}
66+
[5]=>
67+
object(DOMAttr)#12 (2) {
68+
["specified"]=>
69+
bool(true)
70+
["schemaTypeInfo"]=>
71+
NULL
72+
}
73+
[6]=>
74+
object(DOMText)#15 (0) {
75+
}
4976
}

0 commit comments

Comments
 (0)