Skip to content

Commit

Permalink
fix memory leak with creating nodes with a namespace
Browse files Browse the repository at this point in the history
fixes #1771
  • Loading branch information
flavorjones committed Jun 30, 2018
1 parent 117ca2e commit 0d26561
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,10 @@
# unreleased

## Bug fixes

* [MRI] Fix memory leak when creating nodes with namespaces. [#1771]


# 1.8.3 / 2018-06-16

## Security Notes
Expand Down
14 changes: 11 additions & 3 deletions ext/nokogiri/xml_node.c
Expand Up @@ -30,18 +30,23 @@ typedef xmlNodePtr (*pivot_reparentee_func)(xmlNodePtr, xmlNodePtr);
/* :nodoc: */
static void relink_namespace(xmlNodePtr reparented)
{
xmlChar *name, *prefix;
xmlNodePtr child;
xmlNsPtr ns;

if (reparented->type != XML_ATTRIBUTE_NODE &&
reparented->type != XML_ELEMENT_NODE) { return; }

if (reparented->ns == NULL || reparented->ns->prefix == NULL) {
xmlChar *name = 0, *prefix = 0;

name = xmlSplitQName2(reparented->name, &prefix);

if(reparented->type == XML_ATTRIBUTE_NODE) {
if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) { return; }
if (reparented->type == XML_ATTRIBUTE_NODE) {
if (prefix == NULL || strcmp((char*)prefix, XMLNS_PREFIX) == 0) {
xmlFree(name);
xmlFree(prefix);
return;
}
}

ns = xmlSearchNs(reparented->doc, reparented, prefix);
Expand All @@ -54,6 +59,9 @@ static void relink_namespace(xmlNodePtr reparented)
xmlNodeSetName(reparented, name);
xmlSetNs(reparented, ns);
}

xmlFree(name);
xmlFree(prefix);
}

/* Avoid segv when relinking against unlinked nodes. */
Expand Down

0 comments on commit 0d26561

Please sign in to comment.