Skip to content

Commit

Permalink
tree: Fix copying of DTDs
Browse files Browse the repository at this point in the history
- Don't create multiple DTD nodes.
- Fix UAF if malloc fails.
- Skip DTD nodes if tree module is disabled.

Fixes #583.
  • Loading branch information
nwellnhof committed Aug 23, 2023
1 parent 4e4c89a commit d39f780
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -4471,29 +4471,28 @@ xmlNodePtr
xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
xmlNodePtr ret = NULL;
xmlNodePtr p = NULL,q;
xmlDtdPtr newSubset = NULL;

while (node != NULL) {
#ifdef LIBXML_TREE_ENABLED
if (node->type == XML_DTD_NODE ) {
if (doc == NULL) {
#ifdef LIBXML_TREE_ENABLED
if ((doc == NULL) || (doc->intSubset != NULL)) {
node = node->next;
continue;
}
if (doc->intSubset == NULL) {
q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
if (q == NULL) goto error;
q->doc = doc;
q->parent = parent;
doc->intSubset = (xmlDtdPtr) q;
xmlAddChild(parent, q);
} else {
q = (xmlNodePtr) doc->intSubset;
xmlAddChild(parent, q);
}
} else
q = (xmlNodePtr) xmlCopyDtd( (xmlDtdPtr) node );
if (q == NULL) goto error;
q->doc = doc;
q->parent = parent;
newSubset = (xmlDtdPtr) q;
#else
node = node->next;
continue;
#endif /* LIBXML_TREE_ENABLED */
} else {
q = xmlStaticCopyNode(node, doc, parent, 1);
if (q == NULL) goto error;
if (q == NULL) goto error;
}
if (ret == NULL) {
q->prev = NULL;
ret = p = q;
Expand All @@ -4505,6 +4504,8 @@ xmlStaticCopyNodeList(xmlNodePtr node, xmlDocPtr doc, xmlNodePtr parent) {
}
node = node->next;
}
if (newSubset != NULL)
doc->intSubset = newSubset;
return(ret);
error:
xmlFreeNodeList(ret);
Expand Down

0 comments on commit d39f780

Please sign in to comment.