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
Родитель 83a5e2bb5c
Коммит 99d8c4832a
2 изменённых файлов: 16 добавлений и 11 удалений

20
re.c
Просмотреть файл

@ -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 : \

Просмотреть файл

@ -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) }