diff --git a/re.c b/re.c index 3efd540359..44418ec064 100644 --- a/re.c +++ b/re.c @@ -1912,6 +1912,7 @@ match_captures(VALUE match) static int name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end) { + if (NIL_P(regexp)) return -1; return onig_name_to_backref_number(RREGEXP_PTR(regexp), (const unsigned char *)name, (const unsigned char *)name_end, regs); } diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 39577bd75d..231fd392d1 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -161,6 +161,10 @@ class TestRegexp < Test::Unit::TestCase s = "foo" s[/(?o)/, "bar"] = "baz" assert_equal("fbazo", s) + + /.*/ =~ "abc" + "a".sub("a", "") + assert_raise(IndexError) {Regexp.last_match(:_id)} end def test_named_capture_with_nul