Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a53c54

Browse files
alyssaiskou
authored andcommitted
Fix crash with nil XPath variables (#13)
Patch by Alyssa Ross. Thanks!!!
1 parent e4f7b74 commit 2a53c54

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/rexml/functions.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ def Functions::get_namespace( node_set = nil )
135135
#
136136
# An object of a type other than the four basic types is converted to a
137137
# string in a way that is dependent on that type.
138-
def Functions::string( object=nil )
139-
object = @@context[:node] if object.nil?
138+
def Functions::string( object=@@context[:node] )
140139
if object.respond_to?(:node_type)
141140
case object.node_type
142141
when :attribute
@@ -165,8 +164,6 @@ def Functions::string( object=nil )
165164
object.to_s
166165
end
167166
end
168-
when nil
169-
""
170167
else
171168
object.to_s
172169
end

test/rexml/test_functions.rb

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module REXMLTests
77
class FunctionsTester < Test::Unit::TestCase
88
include REXML
9+
10+
def setup
11+
super
12+
REXML::Functions.context = nil
13+
end
14+
915
def test_functions
1016
# trivial text() test
1117
# confuse-a-function
@@ -222,6 +228,19 @@ def test_normalize_space
222228
assert_equal( [REXML::Comment.new("COMMENT A")], m )
223229
end
224230

231+
def test_string_nil_without_context
232+
doc = REXML::Document.new(<<-XML)
233+
<?xml version="1.0" encoding="UTF-8"?>
234+
<root>
235+
<foo bar="baz"/>
236+
<foo bar=""/>
237+
</root>
238+
XML
239+
240+
m = REXML::XPath.match(doc, "//foo[@bar=$n]", nil, { "n" => nil })
241+
assert_equal( 1, m.size )
242+
end
243+
225244
def test_unregistered_method
226245
doc = Document.new("<root/>")
227246
assert_nil(XPath::first(doc.root, "to_s()"))

0 commit comments

Comments
 (0)