Skip to content

Commit 73a3fc3

Browse files
committed
merge revision(s) r46345,r46346: [Backport ruby#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 ruby#9903] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 8a504f7 commit 73a3fc3

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon Aug 4 01:11:07 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* re.c (match_aref, rb_reg_regsub): consider encoding of captured
4+
names, encoding-incompatible should not match.
5+
[ruby-dev:48278] [Bug #9903]
6+
17
Mon Aug 4 00:52:42 2014 Koichi Sasada <[email protected]>
28

39
* vm_eval.c (rb_catch_protect): fix same problem of [Bug #9961].

re.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,20 +1691,16 @@ match_captures(VALUE match)
16911691
static int
16921692
name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
16931693
{
1694-
int num;
1695-
1696-
num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
1694+
return onig_name_to_backref_number(RREGEXP(regexp)->ptr,
16971695
(const unsigned char* )name, (const unsigned char* )name_end, regs);
1698-
if (num >= 1) {
1699-
return num;
1700-
}
1701-
else {
1702-
VALUE s = rb_str_new(name, (long )(name_end - name));
1703-
rb_raise(rb_eIndexError, "undefined group name reference: %s",
1704-
StringValuePtr(s));
1705-
}
1696+
}
17061697

1707-
UNREACHABLE;
1698+
NORETURN(static void name_to_backref_error(VALUE name));
1699+
static void
1700+
name_to_backref_error(VALUE name)
1701+
{
1702+
rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
1703+
name);
17081704
}
17091705

17101706
/*
@@ -1758,8 +1754,11 @@ match_aref(int argc, VALUE *argv, VALUE match)
17581754
/* fall through */
17591755
case T_STRING:
17601756
p = StringValuePtr(idx);
1761-
num = name_to_backref_number(RMATCH_REGS(match),
1762-
RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
1757+
if (!rb_enc_compatible(RREGEXP(RMATCH(match)->regexp)->src, idx) ||
1758+
(num = name_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp,
1759+
p, p + RSTRING_LEN(idx))) < 1) {
1760+
name_to_backref_error(idx);
1761+
}
17631762
return rb_reg_nth_match(num, match);
17641763

17651764
default:
@@ -3369,7 +3368,12 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
33693368
name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
33703369
}
33713370
if (name_end < e) {
3372-
no = name_to_backref_number(regs, regexp, name, name_end);
3371+
VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
3372+
(long)(name_end - name));
3373+
if (!rb_enc_compatible(RREGEXP(regexp)->src, n) ||
3374+
(no = name_to_backref_number(regs, regexp, name, name_end)) < 1) {
3375+
name_to_backref_error(n);
3376+
}
33733377
p = s = name_end + clen;
33743378
break;
33753379
}

test/ruby/test_regexp.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ def test_named_capture_with_nul
158158
}
159159
end
160160

161+
def test_named_capture_nonascii
162+
bug9903 = '[ruby-dev:48278] [Bug #9903]'
163+
164+
key = "\xb1\xb2".force_encoding(Encoding::EUC_JP)
165+
m = /(?<#{key}>.*)/.match("xxx")
166+
assert_equal("xxx", m[key])
167+
assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]}
168+
end
169+
161170
def test_assign_named_capture
162171
assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
163172
assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.2"
22
#define RUBY_RELEASE_DATE "2014-08-04"
3-
#define RUBY_PATCHLEVEL 190
3+
#define RUBY_PATCHLEVEL 191
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 8

0 commit comments

Comments
 (0)