Skip to content

Commit

Permalink
Reset xmlXPathContext state variables before each evaluation
Browse files Browse the repository at this point in the history
If we're re-using the xmlXPathContext object, there's a chance that
the context variables will be trashed by recursive custom functions.

In #3378 (comment),
Nick advised:

> Note that if you use a single XPath context and support custom XPath
> extension functions, a custom function could evaluate XPath
> expressions recursively which will lead to corruption of context
> variables. This is mostly due to some design mistakes in libxml2.

So let's set these context variables back to their default.
  • Loading branch information
flavorjones committed Dec 20, 2024
1 parent f769a04 commit a9315ea
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ noko_xml_xpath_context_set_node(VALUE rb_context, VALUE rb_node)
c_context->doc = c_node->doc;
c_context->node = c_node;

/* Note from @nwellnof in https://github.com/sparklemotion/nokogiri/pull/3378#issuecomment-2557001734:
*
* > Note that if you use a single XPath context and support custom XPath extension functions, a
* > custom function could evaluate XPath expressions recursively which will lead to corruption of
* > context variables. This is mostly due to some design mistakes in libxml2.
*
* So let's set these context variables back to their default.
*/
c_context->contextSize = -1;
c_context->proximityPosition = -1;

return rb_node;
}

Expand Down

0 comments on commit a9315ea

Please sign in to comment.