Skip to content

Commit 2e12e80

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix GH-13012: DOMNode::isEqualNode() is incorrect when attribute order is different
2 parents f5f44bb + 93951cf commit 2e12e80

File tree

3 files changed

+130
-31
lines changed

3 files changed

+130
-31
lines changed

ext/dom/node.c

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,33 +1478,58 @@ static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other
14781478

14791479
#define PHP_DOM_FUNC_CAT(prefix, suffix) prefix##_##suffix
14801480
/* xmlNode and xmlNs have incompatible struct layouts, i.e. the next field is in a different offset */
1481-
#define PHP_DOM_DEFINE_LIST_EQUALITY_HELPER(type) \
1482-
static size_t PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(const type *node) \
1483-
{ \
1484-
size_t counter = 0; \
1485-
while (node) { \
1486-
counter++; \
1487-
node = node->next; \
1488-
} \
1489-
return counter; \
1490-
} \
1491-
static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check, type)(const type *list1, const type *list2) \
1492-
{ \
1493-
size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1494-
if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1495-
return false; \
1496-
} \
1497-
for (size_t i = 0; i < count; i++) { \
1498-
if (!php_dom_node_is_equal_node((const xmlNode *) list1, (const xmlNode *) list2)) { \
1499-
return false; \
1500-
} \
1501-
list1 = list1->next; \
1502-
list2 = list2->next; \
1503-
} \
1504-
return true; \
1505-
}
1506-
PHP_DOM_DEFINE_LIST_EQUALITY_HELPER(xmlNode)
1507-
PHP_DOM_DEFINE_LIST_EQUALITY_HELPER(xmlNs)
1481+
#define PHP_DOM_DEFINE_LIST_COUNTER_HELPER(type) \
1482+
static size_t PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(const type *node) \
1483+
{ \
1484+
size_t counter = 0; \
1485+
while (node) { \
1486+
counter++; \
1487+
node = node->next; \
1488+
} \
1489+
return counter; \
1490+
}
1491+
#define PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(type) \
1492+
static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_ordered, type)(const type *list1, const type *list2) \
1493+
{ \
1494+
size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1495+
if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1496+
return false; \
1497+
} \
1498+
for (size_t i = 0; i < count; i++) { \
1499+
if (!php_dom_node_is_equal_node((const xmlNode *) list1, (const xmlNode *) list2)) { \
1500+
return false; \
1501+
} \
1502+
list1 = list1->next; \
1503+
list2 = list2->next; \
1504+
} \
1505+
return true; \
1506+
}
1507+
#define PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(type) \
1508+
static bool PHP_DOM_FUNC_CAT(php_dom_node_list_equality_check_unordered, type)(const type *list1, const type *list2)\
1509+
{ \
1510+
size_t count = PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list1); \
1511+
if (count != PHP_DOM_FUNC_CAT(php_dom_node_count_list_size, type)(list2)) { \
1512+
return false; \
1513+
} \
1514+
for (const type *n1 = list1; n1 != NULL; n1 = n1->next) { \
1515+
bool found = false; \
1516+
for (const type *n2 = list2; n2 != NULL && !found; n2 = n2->next) { \
1517+
if (php_dom_node_is_equal_node((const xmlNode *) n1, (const xmlNode *) n2)) { \
1518+
found = true; \
1519+
} \
1520+
} \
1521+
if (!found) { \
1522+
return false; \
1523+
} \
1524+
} \
1525+
return true; \
1526+
}
1527+
1528+
PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNode)
1529+
PHP_DOM_DEFINE_LIST_COUNTER_HELPER(xmlNs)
1530+
PHP_DOM_DEFINE_LIST_EQUALITY_ORDERED_HELPER(xmlNode)
1531+
PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNode)
1532+
PHP_DOM_DEFINE_LIST_EQUALITY_UNORDERED_HELPER(xmlNs)
15081533

15091534
static bool php_dom_is_equal_attr(const xmlAttr *this_attr, const xmlAttr *other_attr)
15101535
{
@@ -1532,9 +1557,9 @@ static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other
15321557
&& php_dom_node_is_ns_prefix_equal(this, other)
15331558
&& php_dom_node_is_ns_uri_equal(this, other)
15341559
/* Check attributes first, then namespace declarations, then children */
1535-
&& php_dom_node_list_equality_check_xmlNode((const xmlNode *) this->properties, (const xmlNode *) other->properties)
1536-
&& php_dom_node_list_equality_check_xmlNs(this->nsDef, other->nsDef)
1537-
&& php_dom_node_list_equality_check_xmlNode(this->children, other->children);
1560+
&& php_dom_node_list_equality_check_unordered_xmlNode((const xmlNode *) this->properties, (const xmlNode *) other->properties)
1561+
&& php_dom_node_list_equality_check_unordered_xmlNs(this->nsDef, other->nsDef)
1562+
&& php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children);
15381563
} else if (this->type == XML_DTD_NODE) {
15391564
/* Note: in the living spec entity declarations and notations are no longer compared because they're considered obsolete. */
15401565
const xmlDtd *this_dtd = (const xmlDtd *) this;
@@ -1565,7 +1590,7 @@ static bool php_dom_node_is_equal_node(const xmlNode *this, const xmlNode *other
15651590
const xmlNs *other_ns = (const xmlNs *) other;
15661591
return xmlStrEqual(this_ns->prefix, other_ns->prefix) && xmlStrEqual(this_ns->href, other_ns->href);
15671592
} else if (this->type == XML_DOCUMENT_FRAG_NODE || this->type == XML_HTML_DOCUMENT_NODE || this->type == XML_DOCUMENT_NODE) {
1568-
return php_dom_node_list_equality_check_xmlNode(this->children, other->children);
1593+
return php_dom_node_list_equality_check_ordered_xmlNode(this->children, other->children);
15691594
}
15701595

15711596
return false;

ext/dom/tests/gh13012.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-13012 (DOMNode::isEqualNode() is incorrect when attribute order is different)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument;
9+
$dom->loadXML("<root><x a='a' b='b'/><x b='b' a='a'/><x b='b' a='a' c='c'/></root>");
10+
11+
foreach ($dom->getElementsByTagName('x') as $x1) {
12+
foreach ($dom->getElementsByTagName('x') as $x2) {
13+
echo "Comparing ", $dom->saveXML($x1), " with ", $dom->saveXML($x2), "\n";
14+
var_dump($x1->isEqualNode($x2));
15+
}
16+
}
17+
18+
?>
19+
--EXPECT--
20+
Comparing <x a="a" b="b"/> with <x a="a" b="b"/>
21+
bool(true)
22+
Comparing <x a="a" b="b"/> with <x b="b" a="a"/>
23+
bool(true)
24+
Comparing <x a="a" b="b"/> with <x b="b" a="a" c="c"/>
25+
bool(false)
26+
Comparing <x b="b" a="a"/> with <x a="a" b="b"/>
27+
bool(true)
28+
Comparing <x b="b" a="a"/> with <x b="b" a="a"/>
29+
bool(true)
30+
Comparing <x b="b" a="a"/> with <x b="b" a="a" c="c"/>
31+
bool(false)
32+
Comparing <x b="b" a="a" c="c"/> with <x a="a" b="b"/>
33+
bool(false)
34+
Comparing <x b="b" a="a" c="c"/> with <x b="b" a="a"/>
35+
bool(false)
36+
Comparing <x b="b" a="a" c="c"/> with <x b="b" a="a" c="c"/>
37+
bool(true)

ext/dom/tests/gh13012_ns.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
GH-13012 (DOMNode::isEqualNode() is incorrect when attribute order is different - ns variation)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument;
9+
$dom->loadXML("<root><x/><x xmlns:a=\"urn:a\" xmlns:b=\"urn:b\"/><x xmlns:b=\"urn:b\" xmlns:a=\"urn:a\"/></root>");
10+
11+
foreach ($dom->getElementsByTagName('x') as $x1) {
12+
foreach ($dom->getElementsByTagName('x') as $x2) {
13+
echo "Comparing ", $dom->saveXML($x1), " with ", $dom->saveXML($x2), "\n";
14+
var_dump($x1->isEqualNode($x2));
15+
}
16+
}
17+
18+
?>
19+
--EXPECT--
20+
Comparing <x/> with <x/>
21+
bool(true)
22+
Comparing <x/> with <x xmlns:a="urn:a" xmlns:b="urn:b"/>
23+
bool(false)
24+
Comparing <x/> with <x xmlns:b="urn:b" xmlns:a="urn:a"/>
25+
bool(false)
26+
Comparing <x xmlns:a="urn:a" xmlns:b="urn:b"/> with <x/>
27+
bool(false)
28+
Comparing <x xmlns:a="urn:a" xmlns:b="urn:b"/> with <x xmlns:a="urn:a" xmlns:b="urn:b"/>
29+
bool(true)
30+
Comparing <x xmlns:a="urn:a" xmlns:b="urn:b"/> with <x xmlns:b="urn:b" xmlns:a="urn:a"/>
31+
bool(true)
32+
Comparing <x xmlns:b="urn:b" xmlns:a="urn:a"/> with <x/>
33+
bool(false)
34+
Comparing <x xmlns:b="urn:b" xmlns:a="urn:a"/> with <x xmlns:a="urn:a" xmlns:b="urn:b"/>
35+
bool(true)
36+
Comparing <x xmlns:b="urn:b" xmlns:a="urn:a"/> with <x xmlns:b="urn:b" xmlns:a="urn:a"/>
37+
bool(true)

0 commit comments

Comments
 (0)