From ee8985ef0e35183d606af3a7360cd4713069e9b2 Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Sat, 9 Feb 2019 20:33:06 +0000 Subject: [PATCH] [ruby/rexml] Fix crash with nil XPath variables (#13) Patch by Alyssa Ross. Thanks!!! https://github.com/ruby/rexml/commit/2a53c54f58 --- lib/rexml/functions.rb | 5 +---- test/rexml/test_functions.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb index 219f9c8db5..9c226d2cdf 100644 --- a/lib/rexml/functions.rb +++ b/lib/rexml/functions.rb @@ -135,8 +135,7 @@ module REXML # # An object of a type other than the four basic types is converted to a # string in a way that is dependent on that type. - def Functions::string( object=nil ) - object = @@context[:node] if object.nil? + def Functions::string( object=@@context[:node] ) if object.respond_to?(:node_type) case object.node_type when :attribute @@ -165,8 +164,6 @@ module REXML object.to_s end end - when nil - "" else object.to_s end diff --git a/test/rexml/test_functions.rb b/test/rexml/test_functions.rb index a77be38cc1..c1716ebe1c 100644 --- a/test/rexml/test_functions.rb +++ b/test/rexml/test_functions.rb @@ -6,6 +6,12 @@ require "rexml/document" module REXMLTests class FunctionsTester < Test::Unit::TestCase include REXML + + def setup + super + REXML::Functions.context = nil + end + def test_functions # trivial text() test # confuse-a-function @@ -222,6 +228,19 @@ module REXMLTests assert_equal( [REXML::Comment.new("COMMENT A")], m ) end + def test_string_nil_without_context + doc = REXML::Document.new(<<-XML) + + + + + + XML + + m = REXML::XPath.match(doc, "//foo[@bar=$n]", nil, { "n" => nil }) + assert_equal( 1, m.size ) + end + def test_unregistered_method doc = Document.new("") assert_nil(XPath::first(doc.root, "to_s()"))