From f769a043486bf1aab8ce8a488122556d7cc723be Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Mon, 16 Dec 2024 12:56:19 -0500 Subject: [PATCH 1/2] doc: fix typo in CHANGELOG [skip ci] --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30b3c04490..7effa5c588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA --- -## 1.18.0.rc1 / 2024-12-16 +## v1.18.0.rc1 / 2024-12-16 ### Notable Changes From a9315ea956fcf293c563122c781e6961b8232084 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 20 Dec 2024 13:35:05 -0500 Subject: [PATCH 2/2] Reset xmlXPathContext state variables before each evaluation If we're re-using the xmlXPathContext object, there's a chance that the context variables will be trashed by recursive custom functions. In https://github.com/sparklemotion/nokogiri/pull/3378#issuecomment-2557001734, 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. --- ext/nokogiri/xml_xpath_context.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ext/nokogiri/xml_xpath_context.c b/ext/nokogiri/xml_xpath_context.c index c142273d49..95a8fa00f5 100644 --- a/ext/nokogiri/xml_xpath_context.c +++ b/ext/nokogiri/xml_xpath_context.c @@ -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; }