Skip to content

Commit 947e319

Browse files
committed
Fix phpGH-16594: Assertion failure in DOM -> before
The invalid parent condition can actually happen because PHP's DOM is allows to get children of e.g. attributes; something normally not possible. Closes phpGH-16597.
1 parent 38e1b0a commit 947e319

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ PHP NEWS
88
. Fixed bug GH-16577 (EG(strtod_state).freelist leaks with opcache.preload).
99
(nielsdos)
1010

11+
- DOM:
12+
. Fixed bug GH-16594 (Assertion failure in DOM -> before). (nielsdos)
13+
1114
- GD:
1215
. Fixed bug GH-16559 (UBSan abort in ext/gd/libgd/gd_interpolation.c:1007).
1316
(nielsdos)

ext/dom/parentnode/tree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ static bool dom_is_pre_insert_valid_without_step_1(php_libxml_ref_obj *document,
239239
ZEND_ASSERT(parentNode != NULL);
240240

241241
/* 1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
242-
* => Impossible */
243-
ZEND_ASSERT(!php_dom_pre_insert_is_parent_invalid(parentNode));
242+
* => This is possible because we can grab children of attributes etc... (see e.g. GH-16594) */
243+
if (php_dom_pre_insert_is_parent_invalid(parentNode)) {
244+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, dom_get_strict_error(document));
245+
return false;
246+
}
244247

245248
if (node->doc != documentNode) {
246249
php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(document));

ext/dom/tests/gh16594.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
GH-16594 (Assertion failure in DOM -> before)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$v1 = new DOMText("wr");
9+
$v2 = new DOMDocument();
10+
$v6 = new DOMComment("aw");
11+
$v7 = new DOMAttr("r", "iL");
12+
13+
$v9 = $v2->createElement("test");
14+
$v9->setAttributeNodeNS($v7);
15+
$v7->appendChild($v1);
16+
17+
try {
18+
$v1->before($v6);
19+
} catch (DOMException $e) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
23+
?>
24+
--EXPECT--
25+
Hierarchy Request Error

0 commit comments

Comments
 (0)