Skip to content

Fix various document ref pointer mismanagements #16345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,8 @@ PHP_METHOD(DOMElement, setAttributeNode)
xmlUnlinkNode((xmlNodePtr) attrp);
}

if (attrp->doc == NULL && nodep->doc != NULL) {
attrobj->document = intern->document;
php_libxml_increment_doc_ref((php_libxml_node_object *)attrobj, NULL);
if (attrp->doc == NULL && nodep->doc != NULL && intern->document != NULL) {
dom_set_document_ref_pointers_attr(attrp, intern->document);
}

xmlAddChild(nodep, (xmlNodePtr) attrp);
Expand Down
53 changes: 42 additions & 11 deletions ext/dom/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,25 +820,56 @@ int dom_node_text_content_write(dom_object *obj, zval *newval)

/* }}} */

/* Returns true if the node was changed, false otherwise. */
static bool dom_set_document_ref_obj_single(xmlNodePtr node, xmlDocPtr doc, php_libxml_ref_obj *document)
/* Returns true if the node had the same document reference, false otherwise. */
static bool dom_set_document_ref_obj_single(xmlNodePtr node, php_libxml_ref_obj *document)
{
dom_object *childobj = php_dom_object_get_data(node);
if (childobj && !childobj->document) {
if (!childobj) {
return true;
}
if (!childobj->document) {
childobj->document = document;
document->refcount++;
return true;
}
return false;
}

void dom_set_document_ref_pointers_attr(xmlAttrPtr attr, php_libxml_ref_obj *document)
{
ZEND_ASSERT(document != NULL);

dom_set_document_ref_obj_single((xmlNodePtr) attr, document);
for (xmlNodePtr attr_child = attr->children; attr_child; attr_child = attr_child->next) {
dom_set_document_ref_obj_single(attr_child, document);
}
}

static bool dom_set_document_ref_pointers_node(xmlNodePtr node, php_libxml_ref_obj *document)
{
ZEND_ASSERT(document != NULL);

if (!dom_set_document_ref_obj_single(node, document)) {
return false;
}

if (node->type == XML_ELEMENT_NODE) {
for (xmlAttrPtr attr = node->properties; attr; attr = attr->next) {
dom_set_document_ref_pointers_attr(attr, document);
}
}

return true;
}

/* TODO: on 8.4 replace the loop with the tree walk helper function. */
static void dom_set_document_pointers(xmlNodePtr node, xmlDocPtr doc, php_libxml_ref_obj *document)
void dom_set_document_ref_pointers(xmlNodePtr node, php_libxml_ref_obj *document)
{
/* Applies the document to the entire subtree. */
xmlSetTreeDoc(node, doc);
if (!document) {
return;
}

if (!dom_set_document_ref_obj_single(node, doc, document)) {
if (!dom_set_document_ref_pointers_node(node, document)) {
return;
}

Expand All @@ -847,7 +878,7 @@ static void dom_set_document_pointers(xmlNodePtr node, xmlDocPtr doc, php_libxml
while (node != NULL) {
ZEND_ASSERT(node != base);

if (!dom_set_document_ref_obj_single(node, doc, document)) {
if (!dom_set_document_ref_pointers_node(node, document)) {
break;
}

Expand Down Expand Up @@ -974,7 +1005,7 @@ PHP_METHOD(DOMNode, insertBefore)
}

if (child->doc == NULL && parentp->doc != NULL) {
dom_set_document_pointers(child, parentp->doc, intern->document);
dom_set_document_ref_pointers(child, intern->document);
}

php_libxml_invalidate_node_list_cache(intern->document);
Expand Down Expand Up @@ -1149,7 +1180,7 @@ PHP_METHOD(DOMNode, replaceChild)
}

if (newchild->doc == NULL && nodep->doc != NULL) {
dom_set_document_pointers(newchild, nodep->doc, intern->document);
dom_set_document_ref_pointers(newchild, intern->document);
}

if (newchild->type == XML_DOCUMENT_FRAG_NODE) {
Expand Down Expand Up @@ -1252,7 +1283,7 @@ PHP_METHOD(DOMNode, appendChild)
}

if (child->doc == NULL && nodep->doc != NULL) {
dom_set_document_pointers(child, nodep->doc, intern->document);
dom_set_document_ref_pointers(child, intern->document);
}

if (child->parent != NULL){
Expand Down
2 changes: 2 additions & 0 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ bool php_dom_is_node_connected(const xmlNode *node);
bool php_dom_adopt_node(xmlNodePtr nodep, dom_object *dom_object_new_document, xmlDocPtr new_document);
xmlNsPtr dom_get_ns_resolve_prefix_conflict(xmlNodePtr tree, const char *uri);
xmlEntityPtr dom_entity_reference_fetch_and_sync_declaration(xmlNodePtr reference);
void dom_set_document_ref_pointers(xmlNodePtr node, php_libxml_ref_obj *document);
void dom_set_document_ref_pointers_attr(xmlAttrPtr attr, php_libxml_ref_obj *document);

/* parentnode */
void dom_parent_node_prepend(dom_object *context, zval *nodes, uint32_t nodesc);
Expand Down
18 changes: 18 additions & 0 deletions ext/dom/tests/gh16336_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-16336 (Attribute intern document mismanagement)
--EXTENSIONS--
dom
--FILE--
<?php

$doc = new DOMDocument();
$elem = new DOMElement("g");
$attr = new DOMAttr("iF", "j");

$doc->appendChild($elem);
$elem->setAttributeNode($attr);
echo $attr->firstChild->textContent;

?>
--EXPECT--
j
18 changes: 18 additions & 0 deletions ext/dom/tests/gh16336_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-16336 (Attribute intern document mismanagement)
--EXTENSIONS--
dom
--FILE--
<?php

$doc = new DOMDocument();
$elem = new DOMElement("g");
$attr = new DOMAttr("iF", "j");

$elem->setAttributeNode($attr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it took consulting an external tool (https://www.diffchecker.com/) to figure out that the difference between the _1 and _2 tests was the order of these two statements - suggest adding a comment to make it clearer to the next person that passes along

$doc->appendChild($elem);
echo $attr->firstChild->textContent;

?>
--EXPECT--
j
27 changes: 27 additions & 0 deletions ext/dom/tests/gh16338.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
GH-16338 (Null-dereference in ext/dom/node.c)
--EXTENSIONS--
dom
--CREDITS--
chibinz
--FILE--
<?php
$ref = new DOMEntityReference("G");
$com = new DOMComment();
$doc = new DOMDocument();
$elem = new DOMElement("Rj", "o");
$com2 = new DOMComment();
$elem2 = new DOMElement("kx", null, "r");

$elem2->prepend($com);
$com->before("Z");
$com->before($com2);
$com2->after($elem);
$doc->insertBefore($elem2);
$elem->insertBefore($ref);

echo $doc->saveXML();
?>
--EXPECT--
<?xml version="1.0"?>
<kx xmlns="r">Z<Rj>o&G;</Rj></kx>
Loading