Skip to content

Commit 021ed7d

Browse files
committed
fix: update node GC lifecycle to avoid leaking fragment nodes
Upstream GNOME/libxml2@7e462425 updated `xmlAddChild` to do nothing if the subject node's `->next` or `->prev` are non-NULL. For fragments that are parsed in-node-context, the nodes are parsed as siblings of each other, meaning that unless we explicitly set `next` and `prev` to NULL, the node will not actually be inserted into the tree by `xml_document.c:dealloc_node_i2` and so `xmlFreeDoc` will not be able to free those nodes, resulting in a leak.
1 parent a53df25 commit 021ed7d

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA
1616

1717
* [CRuby] libgumbo (the HTML5 parser) treats reaching max-depth as EOF. This addresses a class of issues when the parser is interrupted in this way. [#3121] @stevecheckoway
1818
* `Node#clone`, `NodeSet#clone`, and `*::Document#clone` all properly copy the metaclass of the original as expected. Previously, `#clone` had been aliased to `#dup` for these classes (since v1.3.0 in 2009). [#316, #3117] @flavorjones
19+
* [CRuby] Update node GC lifecycle to avoid a potential memory leak with fragments in libxml 2.13.0 caused by changes in `xmlAddChild`. [#3156] @flavorjones
1920

2021

2122
### Changed

ext/nokogiri/xml_document.c

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dealloc_node_i2(xmlNodePtr key, xmlNodePtr node, xmlDocPtr doc)
1717
break;
1818
default:
1919
if (node->parent == NULL) {
20+
node->next = NULL;
21+
node->prev = NULL;
2022
xmlAddChild((xmlNodePtr)doc, node);
2123
}
2224
}

0 commit comments

Comments
 (0)