Skip to content
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

Fix crash in ParentNode::append() when dealing with a fragment containing text nodes #14206

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
22 changes: 18 additions & 4 deletions ext/dom/parentnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ static xmlDocPtr dom_doc_from_context_node(xmlNodePtr contextNode)
}
}

/* Citing from the docs (https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/libxml2-tree.html#xmlAddChild):
* "Add a new node to @parent, at the end of the child (or property) list merging adjacent TEXT nodes (in which case @cur is freed)".
* So we must use a custom way of adding that does not merge. */
static void dom_add_child_without_merging(xmlNodePtr parent, xmlNodePtr child)
Copy link
Member Author

@nielsdos nielsdos May 11, 2024

Choose a reason for hiding this comment

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

Note: this function is just backported from master.

{
if (parent->children == NULL) {
parent->children = child;
} else {
xmlNodePtr last = parent->last;
last->next = child;
child->prev = last;
}
parent->last = child;
child->parent = parent;
}

xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNode, zval *nodes, int nodesc)
{
int i;
Expand Down Expand Up @@ -183,7 +199,7 @@ xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNod
* So we must take a copy if this situation arises to prevent a use-after-free. */
bool will_free = newNode->type == XML_TEXT_NODE && fragment->last && fragment->last->type == XML_TEXT_NODE;
if (will_free) {
newNode = xmlCopyNode(newNode, 1);
newNode = xmlCopyNode(newNode, 0);
}

if (newNode->type == XML_DOCUMENT_FRAG_NODE) {
Expand All @@ -192,9 +208,7 @@ xmlNode* dom_zvals_to_fragment(php_libxml_ref_obj *document, xmlNode *contextNod
while (newNode) {
xmlNodePtr next = newNode->next;
xmlUnlinkNode(newNode);
if (!xmlAddChild(fragment, newNode)) {
goto err;
}
dom_add_child_without_merging(fragment, newNode);
newNode = next;
}
} else if (!xmlAddChild(fragment, newNode)) {
Expand Down
21 changes: 21 additions & 0 deletions ext/dom/tests/ParentNode_append_fragment_text_coalesce.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Text coalesce bug when appending fragment with text nodes
--EXTENSIONS--
dom
--FILE--
<?php
$document = new DOMDocument();
$document->loadXML('<root/>');

$sut = $document->createDocumentFragment();
for($i = 0; $i < 10; $i++) {
$textNode = $document->createTextNode("Node$i");
$sut->append($textNode);
}

$document->documentElement->append($sut);
echo $document->saveXML();
?>
--EXPECT--
<?xml version="1.0"?>
<root>Node0Node1Node2Node3Node4Node5Node6Node7Node8Node9</root>
Loading