Skip to content

Commit f1b41d7

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix GH-16151: Assertion failure in ext/dom/parentnode/tree.c
2 parents 921a1e3 + 341c26f commit f1b41d7

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

ext/dom/node.c

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -785,37 +785,52 @@ static xmlNodePtr dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib, xmlN
785785
}
786786
/* }}} */
787787

788-
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
789-
Since:
790-
*/
791-
static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_object *intern, dom_object *childobj, xmlNodePtr parentp, xmlNodePtr child)
788+
static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNodePtr child, bool stricterror)
792789
{
793-
if (!dom_node_children_valid(parentp)) {
794-
RETURN_FALSE;
795-
}
796-
797-
xmlNodePtr new_child = NULL;
798-
bool stricterror = dom_get_strict_error(intern->document);
799-
800790
if (dom_node_is_read_only(parentp) == SUCCESS ||
801791
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
802792
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
803-
RETURN_FALSE;
793+
return false;
804794
}
805795

806796
if (dom_hierarchy(parentp, child) == FAILURE) {
807797
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
808-
RETURN_FALSE;
798+
return false;
809799
}
810800

811801
if (child->doc != parentp->doc && child->doc != NULL) {
812802
php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
813-
RETURN_FALSE;
803+
return false;
814804
}
815805

816806
if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
817807
/* TODO Drop Warning? */
818808
php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
809+
return false;
810+
}
811+
812+
/* In old DOM only text nodes and entity nodes can be added as children to attributes. */
813+
if (parentp->type == XML_ATTRIBUTE_NODE && child->type != XML_TEXT_NODE && child->type != XML_ENTITY_REF_NODE) {
814+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
815+
return false;
816+
}
817+
818+
return true;
819+
}
820+
821+
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
822+
Since:
823+
*/
824+
static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_object *intern, dom_object *childobj, xmlNodePtr parentp, xmlNodePtr child)
825+
{
826+
if (!dom_node_children_valid(parentp)) {
827+
RETURN_FALSE;
828+
}
829+
830+
xmlNodePtr new_child = NULL;
831+
bool stricterror = dom_get_strict_error(intern->document);
832+
833+
if (!dom_node_check_legacy_insertion_validity(parentp, child, stricterror)) {
819834
RETURN_FALSE;
820835
}
821836

@@ -1245,25 +1260,7 @@ static void dom_node_append_child_legacy(zval *return_value, dom_object *intern,
12451260

12461261
bool stricterror = dom_get_strict_error(intern->document);
12471262

1248-
if (dom_node_is_read_only(nodep) == SUCCESS ||
1249-
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1250-
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1251-
RETURN_FALSE;
1252-
}
1253-
1254-
if (dom_hierarchy(nodep, child) == FAILURE) {
1255-
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1256-
RETURN_FALSE;
1257-
}
1258-
1259-
if (!(child->doc == NULL || child->doc == nodep->doc)) {
1260-
php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1261-
RETURN_FALSE;
1262-
}
1263-
1264-
if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
1265-
/* TODO Drop Warning? */
1266-
php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
1263+
if (!dom_node_check_legacy_insertion_validity(nodep, child, stricterror)) {
12671264
RETURN_FALSE;
12681265
}
12691266

ext/dom/tests/gh16151.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-16151 (Assertion failure in ext/dom/parentnode/tree.c)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$element = new DOMElement("N", "W", "y");
9+
$attr = new DOMAttr("c" , "n");
10+
$doc = new DOMDocument();
11+
$doc->appendChild($element);
12+
$element->setAttributeNodeNS($attr);
13+
14+
try {
15+
$attr->insertBefore(new DOMComment("h"));
16+
} catch (DOMException $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
try {
20+
$attr->appendChild(new DOMComment("h"));
21+
} catch (DOMException $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
$attr->insertBefore($doc->createEntityReference('amp'));
26+
$attr->appendChild($doc->createEntityReference('amp'));
27+
28+
echo $doc->saveXML();
29+
30+
?>
31+
--EXPECT--
32+
Hierarchy Request Error
33+
Hierarchy Request Error
34+
<?xml version="1.0"?>
35+
<N xmlns="y" c="n&amp;&amp;">W</N>

0 commit comments

Comments
 (0)