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

ruby-changes:34970

From: nagachika <ko1@a...>
Date: Mon, 4 Aug 2014 01:21:08 +0900 (JST)
Subject: [ruby-changes:34970] nagachika:r47052 (ruby_2_1): merge revision(s) r46345, r46346: [Backport #9903]

nagachika	2014-08-04 01:20:58 +0900 (Mon, 04 Aug 2014)

  New Revision: 47052

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

  Log:
    merge revision(s) r46345,r46346: [Backport #9903]
    
    re.c: reduce new strings
    
    * re.c (match_aref, rb_reg_regsub): reduce new strings creation
      for exceptions.
    * re.c (match_aref, rb_reg_regsub): consider encoding of captured
      names, encoding-incompatible should not match.
      [ruby-dev:48278] [Bug #9903]

  Modified directories:
    branches/ruby_2_1/
  Modified files:
    branches/ruby_2_1/ChangeLog
    branches/ruby_2_1/re.c
    branches/ruby_2_1/test/ruby/test_regexp.rb
    branches/ruby_2_1/version.h
Index: ruby_2_1/ChangeLog
===================================================================
--- ruby_2_1/ChangeLog	(revision 47051)
+++ ruby_2_1/ChangeLog	(revision 47052)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/ChangeLog#L1
+Mon Aug  4 01:11:07 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* re.c (match_aref, rb_reg_regsub): consider encoding of captured
+	  names, encoding-incompatible should not match.
+	  [ruby-dev:48278] [Bug #9903]
+
 Mon Aug  4 00:52:42 2014  Koichi Sasada  <ko1@a...>
 
 	* vm_eval.c (rb_catch_protect): fix same problem of [Bug #9961].
Index: ruby_2_1/re.c
===================================================================
--- ruby_2_1/re.c	(revision 47051)
+++ ruby_2_1/re.c	(revision 47052)
@@ -1691,20 +1691,16 @@ match_captures(VALUE match) https://github.com/ruby/ruby/blob/trunk/ruby_2_1/re.c#L1691
 static int
 name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
 {
-    int num;
-
-    num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
+    return onig_name_to_backref_number(RREGEXP(regexp)->ptr,
 	(const unsigned char* )name, (const unsigned char* )name_end, regs);
-    if (num >= 1) {
-	return num;
-    }
-    else {
-	VALUE s = rb_str_new(name, (long )(name_end - name));
-	rb_raise(rb_eIndexError, "undefined group name reference: %s",
-				 StringValuePtr(s));
-    }
+}
 
-    UNREACHABLE;
+NORETURN(static void name_to_backref_error(VALUE name));
+static void
+name_to_backref_error(VALUE name)
+{
+    rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
+	     name);
 }
 
 /*
@@ -1758,8 +1754,11 @@ match_aref(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/ruby_2_1/re.c#L1754
 		/* fall through */
 	      case T_STRING:
 		p = StringValuePtr(idx);
-		num = name_to_backref_number(RMATCH_REGS(match),
-					     RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
+		if (!rb_enc_compatible(RREGEXP(RMATCH(match)->regexp)->src, idx) ||
+		    (num = name_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp,
+						  p, p + RSTRING_LEN(idx))) < 1) {
+		    name_to_backref_error(idx);
+		}
 		return rb_reg_nth_match(num, match);
 
 	      default:
@@ -3369,7 +3368,12 @@ rb_reg_regsub(VALUE str, VALUE src, stru https://github.com/ruby/ruby/blob/trunk/ruby_2_1/re.c#L3368
                     name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
                 }
                 if (name_end < e) {
-                    no = name_to_backref_number(regs, regexp, name, name_end);
+		    VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
+					    (long)(name_end - name));
+		    if (!rb_enc_compatible(RREGEXP(regexp)->src, n) ||
+			(no = name_to_backref_number(regs, regexp, name, name_end)) < 1) {
+			name_to_backref_error(n);
+		    }
                     p = s = name_end + clen;
                     break;
                 }
Index: ruby_2_1/version.h
===================================================================
--- ruby_2_1/version.h	(revision 47051)
+++ ruby_2_1/version.h	(revision 47052)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/version.h#L1
 #define RUBY_VERSION "2.1.2"
 #define RUBY_RELEASE_DATE "2014-08-04"
-#define RUBY_PATCHLEVEL 190
+#define RUBY_PATCHLEVEL 191
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 8
Index: ruby_2_1/test/ruby/test_regexp.rb
===================================================================
--- ruby_2_1/test/ruby/test_regexp.rb	(revision 47051)
+++ ruby_2_1/test/ruby/test_regexp.rb	(revision 47052)
@@ -158,6 +158,15 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/ruby/test_regexp.rb#L158
     }
   end
 
+  def test_named_capture_nonascii
+    bug9903 = '[ruby-dev:48278] [Bug #9903]'
+
+    key = "\xb1\xb2".force_encoding(Encoding::EUC_JP)
+    m = /(?<#{key}>.*)/.match("xxx")
+    assert_equal("xxx", m[key])
+    assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]}
+  end
+
   def test_assign_named_capture
     assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
     assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))

Property changes on: ruby_2_1
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r46345-46346


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

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