From 3ab0c9df69917e2948e1341fa640f39b49a47db5 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Sat, 12 Jan 2019 17:43:57 -0500 Subject: [PATCH] ensure we trap XML errors while applying XSLT stylesheet otherwise trivial XML errors will silently result in NULL being returned by xsltApplyStylesheet() and a subsequent segfault Fixes #1802 --- ext/nokogiri/xslt_stylesheet.c | 2 +- test/test_xslt_transforms.rb | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/ext/nokogiri/xslt_stylesheet.c b/ext/nokogiri/xslt_stylesheet.c index d0ad6eaaf1..e98302eb07 100644 --- a/ext/nokogiri/xslt_stylesheet.c +++ b/ext/nokogiri/xslt_stylesheet.c @@ -165,7 +165,7 @@ static VALUE transform(int argc, VALUE* argv, VALUE self) errstr = rb_str_new(0, 0); xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler); - xmlSetGenericErrorFunc(NULL, (xmlGenericErrorFunc)&swallow_superfluous_xml_errors); + xmlSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler); result = xsltApplyStylesheet(wrapper->ss, xml, params); free(params); diff --git a/test/test_xslt_transforms.rb b/test/test_xslt_transforms.rb index 1be9b7ed80..3b06e7f0d3 100644 --- a/test/test_xslt_transforms.rb +++ b/test/test_xslt_transforms.rb @@ -311,4 +311,51 @@ def test_non_html_xslt_transform result = xsl.transform xml assert !result.html? end + + it "should not crash when given XPath 2.0 features" do + # + # https://github.com/sparklemotion/nokogiri/issues/1802 + # + # note that here the XPath 2.0 feature is `decimal`. + # this test case is taken from the example provided in the original issue. + # + xml = <<~EOXML + + + + 48.00 + + + EOXML + + xsl = <<~EOXSL + + + + + + + + + + + + EOXSL + + doc = Nokogiri::XML(xml) + xslt = Nokogiri::XSLT(xsl) + exception = assert_raise(RuntimeError) do + xslt.transform(doc) + end + assert_match(/decimal/, exception.message) + end end