Skip to content

Commit

Permalink
Avoid integer sign conversion issue with max_tree_depth
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed May 24, 2024
1 parent e297d33 commit 5dc8703
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ext/nokogiri/gumbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ common_options(VALUE kwargs) {
GumboOptions options = kGumboDefaultOptions;
options.max_attributes = NUM2INT(values[0]);
options.max_errors = NUM2INT(values[1]);
options.max_tree_depth = NUM2INT(values[2]);

// handle negative values
int depth = NUM2INT(values[2]);
options.max_tree_depth = depth < 0 ? UINT_MAX : (unsigned int)depth;

return options;
}
Expand Down Expand Up @@ -568,14 +571,15 @@ noko_gumbo_s_fragment(int argc, VALUE *argv, VALUE _self)
}

// Perform a fragment parse.
// Add one to the max tree depth to account for the HTML element.
options.max_tree_depth = options.max_tree_depth < 0 ? -1 : (options.max_tree_depth + 1);
options.fragment_context = ctx_tag;
options.fragment_namespace = ctx_ns;
options.fragment_encoding = encoding;
options.quirks_mode = quirks_mode;
options.fragment_context_has_form_ancestor = form;

// Add one to the max tree depth to account for the HTML element.
if (options.max_tree_depth < UINT_MAX) { options.max_tree_depth++; }

GumboOutput *output = perform_parse(&options, tags);
ParseArgs args = {
.output = output,
Expand Down
2 changes: 2 additions & 0 deletions test/html5/test_nokogumbo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def test_max_depth_parse
end

assert(Nokogiri::HTML5(html, max_tree_depth: depth))
assert(Nokogiri::HTML5(html, max_tree_depth: -1))
end

def test_max_depth_fragment
Expand All @@ -278,6 +279,7 @@ def test_max_depth_fragment
end

assert(Nokogiri::HTML5.fragment(html, max_tree_depth: depth))
assert(Nokogiri::HTML5.fragment(html, max_tree_depth: -1))
end

def test_document_encoding
Expand Down

0 comments on commit 5dc8703

Please sign in to comment.