diff options
-rw-r--r-- | re.c | 3 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 4 |
2 files changed, 6 insertions, 1 deletions
@@ -3731,7 +3731,8 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp) if (name_end < e) { VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)), (long)(name_end - name)); - if (!rb_enc_compatible(RREGEXP_SRC(regexp), n) || + if (NIL_P(regexp) || + !rb_enc_compatible(RREGEXP_SRC(regexp), n) || (no = name_to_backref_number(regs, regexp, name, name_end)) < 1) { name_to_backref_error(n); } diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index d2fbd00a1d..4dee245462 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1598,6 +1598,10 @@ CODE assert_equal(S("Abc"), S("abc").sub("a") {m = $~; "A"}) assert_equal(S("a"), m[0]) assert_equal(/a/, m.regexp) + bug = '[ruby-core:78686] [Bug #13042] other than regexp has no name references' + assert_raise_with_message(IndexError, /oops/, bug) { + 'hello'.gsub('hello', '\k<oops>') + } end def test_sub! |