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

ruby-changes:59708

From: Nobuyoshi <ko1@a...>
Date: Thu, 16 Jan 2020 11:34:34 +0900 (JST)
Subject: [ruby-changes:59708] 4f19666e8b (master): `Regexp` in `MatchData` can be `nil`

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

From 4f19666e8b144600e959e4673f79d63f98bd637d 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 3efd540..44418ec 100644
--- a/re.c
+++ b/re.c
@@ -1912,6 +1912,7 @@ match_captures(VALUE match) https://github.com/ruby/ruby/blob/trunk/re.c#L1912
 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 39577bd..231fd39 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/

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