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

ruby-changes:67954

From: Nobuyoshi <ko1@a...>
Date: Sun, 12 Sep 2021 11:17:04 +0900 (JST)
Subject: [ruby-changes:67954] 99d8c4832a (master): Preserve the encoding of the argument in IndexError [Bug #18160]

https://git.ruby-lang.org/ruby.git/commit/?id=99d8c4832a

From 99d8c4832a7133ca52578d015e3ddcfd94820f4a Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 11 Sep 2021 21:43:11 +0900
Subject: Preserve the encoding of the argument in IndexError [Bug #18160]

---
 re.c                     | 20 ++++++++++----------
 test/ruby/test_regexp.rb |  7 ++++++-
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/re.c b/re.c
index e4c98eb..4840ce0 100644
--- a/re.c
+++ b/re.c
@@ -1150,6 +1150,14 @@ match_size(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1150
 }
 
 static int name_to_backref_number(struct re_registers *, VALUE, const char*, const char*);
+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);
+}
 
 static int
 match_backref_number(VALUE match, VALUE backref)
@@ -1169,10 +1177,10 @@ match_backref_number(VALUE match, VALUE backref) https://github.com/ruby/ruby/blob/trunk/re.c#L1177
     }
     name = StringValueCStr(backref);
 
-    num = name_to_backref_number(regs, regexp, name, name + strlen(name));
+    num = name_to_backref_number(regs, regexp, name, name + RSTRING_LEN(backref));
 
     if (num < 1) {
-        rb_raise(rb_eIndexError, "undefined group name reference: %s", name);
+        name_to_backref_error(backref);
     }
 
     return num;
@@ -1924,14 +1932,6 @@ name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name https://github.com/ruby/ruby/blob/trunk/re.c#L1932
 	(const unsigned char *)name, (const unsigned char *)name_end, regs);
 }
 
-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);
-}
-
 #define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end)	\
     (NIL_P(re) ? 0 : \
      !rb_enc_compatible(RREGEXP_SRC(re), (name)) ? 0 : \
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 5ba50b3..af62d94 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -712,11 +712,16 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L712
     test = proc {|&blk| "abc".sub("a", ""); blk.call($~) }
 
     bug10877 = '[ruby-core:68209] [Bug #10877]'
+    bug18160 = '[Bug #18160]'
     test.call {|m| assert_raise_with_message(IndexError, /foo/, bug10877) {m["foo"]} }
     key = "\u{3042}"
     [Encoding::UTF_8, Encoding::Shift_JIS, Encoding::EUC_JP].each do |enc|
       idx = key.encode(enc)
-      test.call {|m| assert_raise_with_message(IndexError, /#{idx}/, bug10877) {m[idx]} }
+      pat = /#{idx}/
+      test.call {|m| assert_raise_with_message(IndexError, pat, bug10877) {m[idx]} }
+      test.call {|m| assert_raise_with_message(IndexError, pat, bug18160) {m.offset(idx)} }
+      test.call {|m| assert_raise_with_message(IndexError, pat, bug18160) {m.begin(idx)} }
+      test.call {|m| assert_raise_with_message(IndexError, pat, bug18160) {m.end(idx)} }
     end
     test.call {|m| assert_equal(/a/, m.regexp) }
     test.call {|m| assert_equal("abc", m.string) }
-- 
cgit v1.1


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

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