[前][次][番号順一覧][スレッド一覧]

ruby-changes:60037

From: Nobuyoshi <ko1@a...>
Date: Thu, 13 Feb 2020 15:23:08 +0900 (JST)
Subject: [ruby-changes:60037] db4d136889 (ruby_2_7): `Regexp` in `MatchData` can be `nil`

https://git.ruby-lang.org/ruby.git/commit/?id=db4d136889

From db4d136889afbf59e69efcfd495fd91cd401f378 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 16 Jan 2020 11:25:43 +0900
Subject: `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]

diff --git a/re.c b/re.c
index b15c3a0..9e9df70 100644
--- a/re.c
+++ b/re.c
@@ -1905,6 +1905,7 @@ match_captures(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1905
 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 a1d49c5..b469d64 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -161,6 +161,10 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L161
     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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]