[前][次][番号順一覧][スレッド一覧]

ruby-changes:34263

From: nobu <ko1@a...>
Date: Wed, 4 Jun 2014 21:24:05 +0900 (JST)
Subject: [ruby-changes:34263] nobu:r46344 (trunk): re.c: fix name with NUL

nobu	2014-06-04 21:23:57 +0900 (Wed, 04 Jun 2014)

  New Revision: 46344

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46344

  Log:
    re.c: fix name with NUL
    
    * re.c (match_aref): should not ignore name after NUL byte.
      [ruby-dev:48275] [Bug #9902]

  Modified files:
    trunk/ChangeLog
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46343)
+++ ChangeLog	(revision 46344)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jun  4 21:23:52 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* re.c (match_aref): should not ignore name after NUL byte.
+	  [ruby-dev:48275] [Bug #9902]
+
 Wed Jun  4 04:08:37 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* vm.c (core_hash_merge_kwd): should return the result hash, which
Index: re.c
===================================================================
--- re.c	(revision 46343)
+++ re.c	(revision 46344)
@@ -1796,17 +1796,13 @@ match_aref(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/re.c#L1796
 
 	    switch (TYPE(idx)) {
 	      case T_SYMBOL:
-		p = rb_id2name(SYM2ID(idx));
-		goto name_to_backref;
-		break;
+		idx = rb_sym2str(idx);
+		/* fall through */
 	      case T_STRING:
 		p = StringValuePtr(idx);
-
-	      name_to_backref:
 		num = name_to_backref_number(RMATCH_REGS(match),
-					     RMATCH(match)->regexp, p, p + strlen(p));
+					     RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
 		return rb_reg_nth_match(num, match);
-		break;
 
 	      default:
 		break;
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 46343)
+++ test/ruby/test_regexp.rb	(revision 46344)
@@ -142,6 +142,22 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L142
     assert_equal("fbazo", s)
   end
 
+  def test_named_capture_with_nul
+    bug9902 = '[ruby-dev:48275] [Bug #9902]'
+
+    m = /(?<a>.*)/.match("foo")
+    assert_raise(IndexError, bug9902) {m["a\0foo"]}
+    assert_raise(IndexError, bug9902) {m["a\0foo".to_sym]}
+
+    m = Regexp.new("(?<foo\0bar>.*)").match("xxx")
+    assert_raise(IndexError, bug9902) {m["foo"]}
+    assert_raise(IndexError, bug9902) {m["foo".to_sym]}
+    assert_nothing_raised(IndexError, bug9902) {
+      assert_equal("xxx", m["foo\0bar"], bug9902)
+      assert_equal("xxx", m["foo\0bar".to_sym], bug9902)
+    }
+  end
+
   def test_assign_named_capture
     assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
     assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]