Skip to content

Commit cd67c09

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix GH-16151: Assertion failure in ext/dom/parentnode/tree.c
2 parents 63e1ebe + 066d18f commit cd67c09

File tree

3 files changed

+72
-38
lines changed

3 files changed

+72
-38
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ PHP NEWS
1616
ext/dom/parentnode/tree.c). (nielsdos)
1717
. Fixed bug GH-16149 (Null pointer dereference in
1818
DOMElement->getAttributeNames()). (nielsdos)
19+
. Fixed bug GH-16151 (Assertion failure in ext/dom/parentnode/tree.c).
20+
(nielsdos)
1921

2022
- JSON:
2123
. Fixed bug GH-15168 (stack overflow in json_encode()). (nielsdos)

ext/dom/node.c

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,39 @@ static xmlNodePtr _php_dom_insert_fragment(xmlNodePtr nodep, xmlNodePtr prevsib,
865865
}
866866
/* }}} */
867867

868+
static bool dom_node_check_legacy_insertion_validity(xmlNodePtr parentp, xmlNodePtr child, bool stricterror)
869+
{
870+
if (dom_node_is_read_only(parentp) == SUCCESS ||
871+
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
872+
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
873+
return false;
874+
}
875+
876+
if (dom_hierarchy(parentp, child) == FAILURE) {
877+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
878+
return false;
879+
}
880+
881+
if (child->doc != parentp->doc && child->doc != NULL) {
882+
php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
883+
return false;
884+
}
885+
886+
if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
887+
/* TODO Drop Warning? */
888+
php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
889+
return false;
890+
}
891+
892+
/* In old DOM only text nodes and entity nodes can be added as children to attributes. */
893+
if (parentp->type == XML_ATTRIBUTE_NODE && child->type != XML_TEXT_NODE && child->type != XML_ENTITY_REF_NODE) {
894+
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
895+
return false;
896+
}
897+
898+
return true;
899+
}
900+
868901
/* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-952280727
869902
Since:
870903
*/
@@ -892,25 +925,7 @@ PHP_METHOD(DOMNode, insertBefore)
892925

893926
stricterror = dom_get_strict_error(intern->document);
894927

895-
if (dom_node_is_read_only(parentp) == SUCCESS ||
896-
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
897-
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
898-
RETURN_FALSE;
899-
}
900-
901-
if (dom_hierarchy(parentp, child) == FAILURE) {
902-
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
903-
RETURN_FALSE;
904-
}
905-
906-
if (child->doc != parentp->doc && child->doc != NULL) {
907-
php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
908-
RETURN_FALSE;
909-
}
910-
911-
if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
912-
/* TODO Drop Warning? */
913-
php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
928+
if (!dom_node_check_legacy_insertion_validity(parentp, child, stricterror)) {
914929
RETURN_FALSE;
915930
}
916931

@@ -1196,25 +1211,7 @@ PHP_METHOD(DOMNode, appendChild)
11961211

11971212
stricterror = dom_get_strict_error(intern->document);
11981213

1199-
if (dom_node_is_read_only(nodep) == SUCCESS ||
1200-
(child->parent != NULL && dom_node_is_read_only(child->parent) == SUCCESS)) {
1201-
php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, stricterror);
1202-
RETURN_FALSE;
1203-
}
1204-
1205-
if (dom_hierarchy(nodep, child) == FAILURE) {
1206-
php_dom_throw_error(HIERARCHY_REQUEST_ERR, stricterror);
1207-
RETURN_FALSE;
1208-
}
1209-
1210-
if (!(child->doc == NULL || child->doc == nodep->doc)) {
1211-
php_dom_throw_error(WRONG_DOCUMENT_ERR, stricterror);
1212-
RETURN_FALSE;
1213-
}
1214-
1215-
if (child->type == XML_DOCUMENT_FRAG_NODE && child->children == NULL) {
1216-
/* TODO Drop Warning? */
1217-
php_error_docref(NULL, E_WARNING, "Document Fragment is empty");
1214+
if (!dom_node_check_legacy_insertion_validity(nodep, child, stricterror)) {
12181215
RETURN_FALSE;
12191216
}
12201217

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)