Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memsize_node when called on xmlAttrs #2924

Merged
merged 3 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

## next / unreleased

...
* [CRuby] `ObjectSpace.memsize_of` is now safe to call on `Document`s with complex DTDs. In previous versions, this debugging method could result in a segfault. [[#2923](https://github.com/sparklemotion/nokogiri/issues/2923), [#2924](https://github.com/sparklemotion/nokogiri/issues/2924)]


## 1.15.3 / 2023-07-05
Expand Down
8 changes: 6 additions & 2 deletions ext/nokogiri/xml_document.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,15 @@ memsize_node(const xmlNodePtr node)
{
/* note we don't count namespace definitions, just going for a good-enough number here */
xmlNodePtr child;
xmlAttrPtr property;
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 (property = node->properties; property; property = property->next) {
memsize += sizeof(xmlAttr) + memsize_node((xmlNodePtr)property);
}
}
if (node->type == XML_TEXT_NODE) {
memsize += xmlStrlen(node->content);
Expand Down
15 changes: 15 additions & 0 deletions test/test_memory_leak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,21 @@ def test_object_space_memsize_of
assert(bigger_name_size > base_size, "longer tags should increase memsize")
end

def test_object_space_memsize_with_dtd
# https://github.com/sparklemotion/nokogiri/issues/2923
require "objspace"
skip("memsize_of not defined") unless ObjectSpace.respond_to?(:memsize_of)

doc = Nokogiri::XML(<<~XML)
<?xml version="1.0"?>
<!DOCTYPE staff PUBLIC "staff.dtd" [
<!ATTLIST payment type CDATA "check">
]>
<staff></staff>
XML
ObjectSpace.memsize_of(doc) # assert_does_not_crash
end

module MemInfo
# from https://stackoverflow.com/questions/7220896/get-current-ruby-process-memory-usage
# this is only going to work on linux
Expand Down