Skip to content

Commit

Permalink
fix memsize_node when called on xmlAttrs
Browse files Browse the repository at this point in the history
The `properties` field of an `xmlNode` element points to an `xmlAttr`.
The first few fields of `xmlAttr` are in common with `xmlNode`, but not
the `properties` field which doesn't exist in an `xmlAttr`.

The `memsize_node` function was passing an `xmlAttr` to a
recursive call and then trying to do the same with the properties of
that.

This led to type confusion and subsequent crashes.

Fixes: sparklemotion#2923
  • Loading branch information
stevecheckoway committed Jul 6, 2023
1 parent 2edbbef commit 81762fa
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ memsize_node(const xmlNodePtr node)
size_t memsize = 0;

memsize += xmlStrlen(node->name);
for (child = (xmlNodePtr)node->properties; child; child = child->next) {
memsize += sizeof(xmlAttr) + memsize_node(child);

if (node->type == XML_ELEMENT_NODE) {
for (child = (xmlNodePtr)node->properties; child; child = child->next) {
memsize += sizeof(xmlAttr) + memsize_node(child);
}
}
if (node->type == XML_TEXT_NODE) {
memsize += xmlStrlen(node->content);
Expand Down

0 comments on commit 81762fa

Please sign in to comment.