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

ruby-changes:71542

From: Jeremy <ko1@a...>
Date: Wed, 30 Mar 2022 07:29:28 +0900 (JST)
Subject: [ruby-changes:71542] 6d3f447aec (master): Fix multiplex backreferencs near end of string in regexp match

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

From 6d3f447aecfb56f7d3edbdf9cc68e748e150d7d8 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Thu, 24 Mar 2022 11:05:12 -0700
Subject: Fix multiplex backreferencs near end of string in regexp match

Idea from Jirka Marsik.

Fixes [Bug #18631]
---
 regexec.c                | 6 ++++--
 test/ruby/test_regexp.rb | 6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/regexec.c b/regexec.c
index 9535e45228..da17c04a55 100644
--- a/regexec.c
+++ b/regexec.c
@@ -1178,11 +1178,13 @@ static int string_cmp_ic(OnigEncoding enc, int case_fold_flag, https://github.com/ruby/ruby/blob/trunk/regexec.c#L1178
 # define DATA_ENSURE_CHECK1    (s < right_range)
 # define DATA_ENSURE_CHECK(n)  (s + (n) <= right_range)
 # define DATA_ENSURE(n)        if (s + (n) > right_range) goto fail
+# define DATA_ENSURE_CONTINUE(n) if (s + (n) > right_range) continue
 # define ABSENT_END_POS        right_range
 #else
 # define DATA_ENSURE_CHECK1    (s < end)
 # define DATA_ENSURE_CHECK(n)  (s + (n) <= end)
 # define DATA_ENSURE(n)        if (s + (n) > end) goto fail
+# define DATA_ENSURE_CONTINUE(n) if (s + (n) > end) continue
 # define ABSENT_END_POS        end
 #endif /* USE_MATCH_RANGE_MUST_BE_INSIDE_OF_SPECIFIED_RANGE */
 
@@ -2645,7 +2647,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, https://github.com/ruby/ruby/blob/trunk/regexec.c#L2647
 		  ? STACK_AT(mem_end_stk[mem])->u.mem.pstr
 		  : (UChar* )((void* )mem_end_stk[mem]));
 	  n = pend - pstart;
-	  DATA_ENSURE(n);
+	  DATA_ENSURE_CONTINUE(n);
 	  sprev = s;
 	  swork = s;
 	  STRING_CMP_VALUE(pstart, swork, n, is_fail);
@@ -2684,7 +2686,7 @@ match_at(regex_t* reg, const UChar* str, const UChar* end, https://github.com/ruby/ruby/blob/trunk/regexec.c#L2686
 		  ? STACK_AT(mem_end_stk[mem])->u.mem.pstr
 		  : (UChar* )((void* )mem_end_stk[mem]));
 	  n = pend - pstart;
-	  DATA_ENSURE(n);
+	  DATA_ENSURE_CONTINUE(n);
 	  sprev = s;
 	  swork = s;
 	  STRING_CMP_VALUE_IC(case_fold_flag, pstart, &swork, n, end, is_fail);
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 4761d1dc39..94098a850d 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -1422,6 +1422,12 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L1422
     end
   end
 
+  def test_bug18631
+    assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaaa")
+    assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaa")
+    assert_kind_of MatchData, /(?<x>a)(?<x>aa)\k<x>/.match("aaaab")
+  end
+
   # This assertion is for porting x2() tests in testpy.py of Onigmo.
   def assert_match_at(re, str, positions, msg = nil)
     re = Regexp.new(re) unless re.is_a?(Regexp)
-- 
cgit v1.2.1


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

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