1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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]
This commit is contained in:
Nobuyoshi Nakada 2020-01-16 11:25:43 +09:00
parent 815807d2ab
commit 4f19666e8b
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 5 additions and 0 deletions

1
re.c
View file

@ -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);
}

View file

@ -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