diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-01-16 11:25:43 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-01-16 11:32:11 +0900 |
commit | 4f19666e8b144600e959e4673f79d63f98bd637d (patch) | |
tree | 00f9f406d2e8fd33f40c0a41701fe49674bf41ae | |
parent | 815807d2ab9e87b5543d6837234952fd6cd481a3 (diff) | |
download | ruby-4f19666e8b144600e959e4673f79d63f98bd637d.tar.gz |
`Regexp` in `MatchData` can be `nil`
`String#sub` with a string pattern defers creating a `Regexp`
until `MatchData#regexp` creates a `Regexp` from the matched
string. `Regexp#last_match(group_name)` accessed its content
without creating the `Regexp` though. [Bug #16508]
-rw-r--r-- | re.c | 1 | ||||
-rw-r--r-- | test/ruby/test_regexp.rb | 4 |
2 files changed, 5 insertions, 0 deletions
@@ -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[/(?<bar>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 |