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

Delay breaking out of the parse loop when max_tree_depth is hit #3100

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions gumbo-parser/src/parser.c
Expand Up @@ -4826,14 +4826,17 @@ GumboOutput* gumbo_parse_with_options (
// to a token.
if (token.type == GUMBO_TOKEN_END_TAG &&
token.v.end_tag.tag == GUMBO_TAG_UNKNOWN)
{
gumbo_free(token.v.end_tag.name);
token.v.end_tag.name = NULL;
}
if (unlikely(state->_open_elements.length > max_tree_depth)) {
parser._output->status = GUMBO_STATUS_TREE_TOO_DEEP;
gumbo_debug("Tree depth limit exceeded.\n");
break;
}
}

if (unlikely(state->_open_elements.length > max_tree_depth)) {
parser._output->status = GUMBO_STATUS_TREE_TOO_DEEP;
gumbo_debug("Tree depth limit exceeded.\n");
break;
}

++loop_count;
assert(loop_count < 1000000000UL);
Expand Down
10 changes: 10 additions & 0 deletions test/test_memory_usage.rb
Expand Up @@ -301,5 +301,15 @@ def start_element(name, attrs = [])
Nokogiri::HTML5::Document.parse(html)
end
end

it "libgumbo max depth exceeded" do
html = "<html><body>"

memwatch(__method__) do
Nokogiri::HTML5.parse(html, max_tree_depth: 1)
rescue ArgumentError
# Expected error. This comment makes rubocop happy.
end
end
end if ENV["NOKOGIRI_MEMORY_SUITE"] && Nokogiri.uses_libxml?
end