Skip to content

Commit 58cead1

Browse files
committed
Fix GH-16593: Assertion failure in DOM->replaceChild
This is already forbidden by libxml, but this condition isn't properly checked; so the return value and lack of error makes it seem like it worked while it actually didn't. Furthermore, this can break assumptions and assertions later on.
1 parent f9ce5e7 commit 58cead1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

ext/dom/node.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,13 @@ PHP_METHOD(DOMNode, replaceChild)
10931093
RETURN_FALSE;
10941094
}
10951095

1096+
/* This is already disallowed by libxml, but we should check it here to avoid
1097+
* breaking assumptions and assertions. */
1098+
if ((oldchild->type == XML_ATTRIBUTE_NODE) != (newchild->type == XML_ATTRIBUTE_NODE)) {
1099+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1100+
RETURN_FALSE;
1101+
}
1102+
10961103
if (oldchild->parent != nodep) {
10971104
php_dom_throw_error(NOT_FOUND_ERR, stricterror);
10981105
RETURN_FALSE;

ext/dom/tests/gh16593.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
GH-16593 (Assertion failure in DOM->replaceChild)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$doc = new DOMDocument;
9+
$root = $doc->appendChild($doc->createElement('root'));
10+
$child = $root->appendChild($doc->createElement('child'));
11+
try {
12+
$root->replaceChild($doc->createAttribute('foo'), $child);
13+
} catch (DOMException $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
echo $doc->saveXML();
17+
18+
?>
19+
--EXPECT--
20+
Hierarchy Request Error
21+
<?xml version="1.0"?>
22+
<root><child/></root>

0 commit comments

Comments
 (0)