Skip to content

Commit d89dd28

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. Closes GH-16596.
1 parent e3de1a1 commit d89dd28

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PHP NEWS
3939
. Fixed bug GH-16533 (Segfault when adding attribute to parent that is not
4040
an element). (nielsdos)
4141
. Fixed bug GH-16535 (UAF when using document as a child). (nielsdos)
42+
. Fixed bug GH-16593 (Assertion failure in DOM->replaceChild). (nielsdos)
4243

4344
- EXIF:
4445
. Fixed bug GH-16409 (Segfault in exif_thumbnail when not dealing with a

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)