Skip to content

Fix crash in adoptNode with attribute references #13002

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
34 changes: 23 additions & 11 deletions ext/dom/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,21 +1041,33 @@ PHP_METHOD(DOMDocument, getElementById)
}
/* }}} end dom_document_get_element_by_id */

static void php_dom_transfer_document_ref(xmlNodePtr node, dom_object *dom_object_document, xmlDocPtr document)
static zend_always_inline void php_dom_transfer_document_ref_single_node(xmlNodePtr node, php_libxml_ref_obj *new_document)
{
php_libxml_node_ptr *iteration_object_ptr = node->_private;
if (iteration_object_ptr) {
php_libxml_node_object *iteration_object = iteration_object_ptr->_private;
ZEND_ASSERT(iteration_object != NULL);
/* Must increase refcount first because we could be the last reference holder, and the document may be equal. */
new_document->refcount++;
php_libxml_decrement_doc_ref(iteration_object);
iteration_object->document = new_document;
}
}

static void php_dom_transfer_document_ref(xmlNodePtr node, php_libxml_ref_obj *new_document)
{
if (node->children) {
php_dom_transfer_document_ref(node->children, dom_object_document, document);
php_dom_transfer_document_ref(node->children, new_document);
}

while (node) {
php_libxml_node_ptr *iteration_object_ptr = node->_private;
if (iteration_object_ptr) {
php_libxml_node_object *iteration_object = iteration_object_ptr->_private;
ZEND_ASSERT(iteration_object != NULL);
/* Must increase refcount first because we could be the last reference holder, and the document may be equal. */
dom_object_document->document->refcount++;
php_libxml_decrement_doc_ref(iteration_object);
iteration_object->document = dom_object_document->document;
if (node->type == XML_ELEMENT_NODE) {
for (xmlAttrPtr attr = node->properties; attr != NULL; attr = attr->next) {
php_dom_transfer_document_ref_single_node((xmlNodePtr) attr, new_document);
}
}

php_dom_transfer_document_ref_single_node(node, new_document);
node = node->next;
}
}
Expand All @@ -1073,7 +1085,7 @@ bool php_dom_adopt_node(xmlNodePtr nodep, dom_object *dom_object_new_document, x
return false;
}

php_dom_transfer_document_ref(nodep, dom_object_new_document, new_document);
php_dom_transfer_document_ref(nodep, dom_object_new_document->document);
} else {
xmlUnlinkNode(nodep);
}
Expand Down
27 changes: 27 additions & 0 deletions ext/dom/tests/DOMDocument_adoptNode_attribute_references.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
DOMDocument::adoptNode() with attribute references
--EXTENSIONS--
dom
--FILE--
<?php

$dom = new DOMDocument;
$root = $dom->appendChild($dom->createElement('root'));
$root->setAttributeNS("urn:a", "a:root1", "bar");
$root1 = $root->getAttributeNodeNS("urn:a", "root1");
echo $dom->saveXML();

$dom = new DOMDocument;
$dom->appendChild($dom->adoptNode($root));
foreach ($dom->documentElement->attributes as $attr) {
var_dump($attr->namespaceURI, $attr->prefix, $attr->localName, $attr->nodeValue);
}

?>
--EXPECT--
<?xml version="1.0"?>
<root xmlns:a="urn:a" a:root1="bar"/>
string(5) "urn:a"
string(1) "a"
string(5) "root1"
string(3) "bar"