diff --git a/ext/nokogiri/gumbo.c b/ext/nokogiri/gumbo.c index e1ed0b8c28e..1295e9085e3 100644 --- a/ext/nokogiri/gumbo.c +++ b/ext/nokogiri/gumbo.c @@ -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; } @@ -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, diff --git a/test/html5/test_nokogumbo.rb b/test/html5/test_nokogumbo.rb index 03922908db8..e2e4d037d46 100644 --- a/test/html5/test_nokogumbo.rb +++ b/test/html5/test_nokogumbo.rb @@ -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 @@ -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