Skip to content

Commit

Permalink
tree: Don't allow misuse of xmlAddChild
Browse files Browse the repository at this point in the history
xmlAddChild assumes that the child is unlinked. If the child is already
linked, return an error instead of corrupting the tree.
  • Loading branch information
nwellnhof committed Mar 15, 2024
1 parent 2c214a5 commit 7e46242
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,9 @@ xmlAddChildList(xmlNodePtr parent, xmlNodePtr cur) {
* If the new node is ATTRIBUTE, it is added into properties instead of children.
* If there is an attribute with equal name, it is first destroyed.
*
* This function assumes that @cur has no siblings. If @cur has a parent,
* it must equal @parent.
*
* All tree manipulation functions can safely move nodes within a document.
* But when moving nodes from one document to another, references to
* namespaces in element or attribute nodes are NOT fixed. In this case,
Expand All @@ -3318,7 +3321,9 @@ xmlAddChild(xmlNodePtr parent, xmlNodePtr cur) {
return(NULL);
}

if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL)) {
if ((cur == NULL) || (cur->type == XML_NAMESPACE_DECL) ||
(cur->next != NULL) || (cur->prev != NULL) ||
((cur->parent != NULL && cur->parent != parent))) {
return(NULL);
}

Expand Down

0 comments on commit 7e46242

Please sign in to comment.