ruby-changes:33379
From: nobu <ko1@a...>
Date: Fri, 28 Mar 2014 11:27:19 +0900 (JST)
Subject: [ruby-changes:33379] nobu:r45458 (trunk): string.c: unset $~ if unmatch
nobu 2014-03-28 11:27:14 +0900 (Fri, 28 Mar 2014) New Revision: 45458 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45458 Log: string.c: unset $~ if unmatch * string.c (rb_pat_search): unset $~ if the last match failed. Modified files: trunk/string.c trunk/test/ruby/test_string.rb Index: string.c =================================================================== --- string.c (revision 45457) +++ string.c (revision 45458) @@ -3893,9 +3893,14 @@ rb_pat_search(VALUE pat, VALUE str, long https://github.com/ruby/ruby/blob/trunk/string.c#L3893 { if (BUILTIN_TYPE(pat) == T_STRING) { pos = rb_str_index(str, pat, pos); - if (pos >= 0 && set_backref_str) { - str = rb_str_new_frozen(str); - rb_backref_set_string(str, pos, RSTRING_LEN(pat)); + if (set_backref_str) { + if (pos >= 0) { + str = rb_str_new_frozen(str); + rb_backref_set_string(str, pos, RSTRING_LEN(pat)); + } + else { + rb_backref_set(Qnil); + } } return pos; } Index: test/ruby/test_string.rb =================================================================== --- test/ruby/test_string.rb (revision 45457) +++ test/ruby/test_string.rb (revision 45458) @@ -1147,6 +1147,14 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L1147 res = [] a.scan(/./) { |w| res << w } assert_predicate(res[0], :tainted?, '[ruby-core:33338] #4087') + + /h/ =~ a + a.scan(/x/) + assert_nil($~) + + /h/ =~ a + a.scan('x') + assert_nil($~) end def test_size -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/