Skip to content

Commit 14d1f5a

Browse files
committed
test: add coverage for the memsize_of bug
which turns out to be because xmlDTD has "attributes" where xmlNode has "properties" (cherry picked from commit 19a64ba)
1 parent c39ec30 commit 14d1f5a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

ext/nokogiri/xml_document.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ memsize_node(const xmlNodePtr node)
100100
{
101101
/* note we don't count namespace definitions, just going for a good-enough number here */
102102
xmlNodePtr child;
103+
xmlAttrPtr property;
103104
size_t memsize = 0;
104105

105106
memsize += xmlStrlen(node->name);
106107

107108
if (node->type == XML_ELEMENT_NODE) {
108-
for (child = (xmlNodePtr)node->properties; child; child = child->next) {
109-
memsize += sizeof(xmlAttr) + memsize_node(child);
109+
for (property = node->properties; property; property = property->next) {
110+
memsize += sizeof(xmlAttr) + memsize_node((xmlNodePtr)property);
110111
}
111112
}
112113
if (node->type == XML_TEXT_NODE) {

test/test_memory_leak.rb

+15
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,21 @@ def test_object_space_memsize_of
313313
assert(bigger_name_size > base_size, "longer tags should increase memsize")
314314
end
315315

316+
def test_object_space_memsize_with_dtd
317+
# https://github.com/sparklemotion/nokogiri/issues/2923
318+
require "objspace"
319+
skip("memsize_of not defined") unless ObjectSpace.respond_to?(:memsize_of)
320+
321+
doc = Nokogiri::XML(<<~XML)
322+
<?xml version="1.0"?>
323+
<!DOCTYPE staff PUBLIC "staff.dtd" [
324+
<!ATTLIST payment type CDATA "check">
325+
]>
326+
<staff></staff>
327+
XML
328+
ObjectSpace.memsize_of(doc) # assert_does_not_crash
329+
end
330+
316331
module MemInfo
317332
# from https://stackoverflow.com/questions/7220896/get-current-ruby-process-memory-usage
318333
# this is only going to work on linux

0 commit comments

Comments
 (0)