Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with nil XPath variables #13

Merged
merged 5 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/rexml/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def Functions::get_namespace( node_set = nil )
#
# 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
Expand Down Expand Up @@ -165,8 +164,6 @@ def Functions::string( object=nil )
object.to_s
end
end
when nil
""
else
object.to_s
end
Expand Down
19 changes: 19 additions & 0 deletions test/rexml/test_functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
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
Expand Down Expand Up @@ -222,6 +228,19 @@ def test_normalize_space
assert_equal( [REXML::Comment.new("COMMENT A")], m )
end

def test_string_nil_without_context
doc = REXML::Document.new(<<-XML)
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo bar="baz"/>
<foo bar=""/>
</root>
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("<root/>")
assert_nil(XPath::first(doc.root, "to_s()"))
Expand Down