1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Preserve the encoding of the argument in IndexError [Bug #18160]

This commit is contained in:
Nobuyoshi Nakada 2021-09-11 21:43:11 +09:00
parent 83a5e2bb5c
commit 99d8c4832a
Notes: git 2021-09-12 11:17:11 +09:00
2 changed files with 16 additions and 11 deletions

20
re.c
View file

@ -1150,6 +1150,14 @@ match_size(VALUE match)
}
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)
}
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
(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 : \

View file

@ -712,11 +712,16 @@ class TestRegexp < Test::Unit::TestCase
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) }