summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-03 03:27:43 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-09-03 03:27:43 +0000
commitd910ce1f908a70afad5261d91fd6dec0a3708240 (patch)
tree72ac3ecb727d68441d804e30fb310a71edf8b16b /re.c
parentb10c3b6d51d7fb0597670d88a537d713b445098e (diff)
merge revision(s) 42251,46345,46346: [Backport #9903]
* sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable characters. * sprintf.c (ruby__sfvextra): add QUOTE flag to escape unprintable characters. * re.c (match_aref, rb_reg_regsub): consider encoding of captured names, encoding-incompatible should not match. [ruby-dev:48278] [Bug #9903] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@47364 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/re.c b/re.c
index a9444ba97f..506d015d1a 100644
--- a/re.c
+++ b/re.c
@@ -1665,20 +1665,16 @@ match_captures(VALUE match)
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);
}
/*
@@ -1732,8 +1728,11 @@ match_aref(int argc, VALUE *argv, VALUE match)
/* 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:
@@ -3342,7 +3341,12 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
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;
}