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

ruby-changes:42995

From: nobu <ko1@a...>
Date: Thu, 19 May 2016 12:10:16 +0900 (JST)
Subject: [ruby-changes:42995] nobu:r55069 (trunk): re.c: fix match?

nobu	2016-05-19 12:10:12 +0900 (Thu, 19 May 2016)

  New Revision: 55069

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55069

  Log:
    re.c: fix match?
    
    * re.c (rb_reg_match_m_p): fix match against empty string.
      rb_str_offset returns the end when the position exceeds the
      length.  fix the range parameter of onig_search.
      [ruby-core:75604] [Bug #12394]

  Modified files:
    trunk/ChangeLog
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: re.c
===================================================================
--- re.c	(revision 55068)
+++ re.c	(revision 55069)
@@ -3227,6 +3227,7 @@ rb_reg_match_m_p(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/re.c#L3227
     regex_t *reg;
     onig_errmsg_buffer err = "";
     OnigPosition result;
+    const UChar *start, *end;
     int tmpreg;
 
     rb_scan_args(argc, argv, "11", &str, &initpos);
@@ -3234,27 +3235,23 @@ rb_reg_match_m_p(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/re.c#L3235
     str = SYMBOL_P(str) ? rb_sym2str(str) : rb_str_to_str(str);
     if (argc == 2) {
 	pos = NUM2LONG(initpos);
-	if (pos == 0) goto run;
 	if (pos < 0) {
 	    pos += NUM2LONG(rb_str_length(str));
-	    if (pos == 0) goto run;
 	    if (pos < 0) return Qfalse;
-
 	}
-	pos = rb_str_offset(str, pos);
-    }
-run:
-    if (pos >= RSTRING_LEN(str)) {
-	return Qfalse;
+	if (pos > 0) {
+	    long len = 0;
+	    char *beg = rb_str_subpos(str, pos, &len);
+	    if (!beg) return Qfalse;
+	    pos = beg - RSTRING_PTR(str);
+	}
     }
     reg = rb_reg_prepare_re0(re, str, err);
     tmpreg = reg != RREGEXP_PTR(re);
     if (!tmpreg) RREGEXP(re)->usecnt++;
-    result = onig_search(reg,
-			 ((UChar*)RSTRING_PTR(str)),
-			 ((UChar*)RSTRING_END(str)),
-			 ((UChar*)(RSTRING_PTR(str)) + pos),
-			 ((UChar*)RSTRING_PTR(str)),
+    start = ((UChar*)RSTRING_PTR(str));
+    end = start + RSTRING_LEN(str);
+    result = onig_search(reg, start, end, start + pos, end,
 			 NULL, ONIG_OPTION_NONE);
     if (!tmpreg) RREGEXP(re)->usecnt--;
     if (tmpreg) {
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 55068)
+++ test/ruby/test_regexp.rb	(revision 55069)
@@ -528,14 +528,21 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L528
 
   def test_match_p
     /backref/ =~ 'backref'
+    # must match here, but not in a separate method, e.g., assert_send,
+    # to check if $~ is affected or not.
     assert_equal(false, //.match?(nil))
+    assert_equal(true, //.match?(""))
     assert_equal(true, /.../.match?(:abc))
     assert_raise(TypeError) { /.../.match?(Object.new) }
+    assert_equal(true, /b/.match?('abc'))
+    assert_equal(true, /b/.match?('abc', 1))
     assert_equal(true, /../.match?('abc', 1))
     assert_equal(true, /../.match?('abc', -2))
     assert_equal(false, /../.match?("abc", -4))
     assert_equal(false, /../.match?("abc", 4))
     assert_equal(true, /../n.match?("\u3042" + '\x', 1))
+    assert_equal(true, /\z/.match?(""))
+    assert_equal(true, /\z/.match?("abc"))
     assert_equal('backref', $&)
   end
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55068)
+++ ChangeLog	(revision 55069)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu May 19 12:10:10 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* re.c (rb_reg_match_m_p): fix match against empty string.
+	  rb_str_offset returns the end when the position exceeds the
+	  length.  fix the range parameter of onig_search.
+	  [ruby-core:75604] [Bug #12394]
+
 Thu May 19 11:37:36 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* re.c (rb_reg_match_m_p): should return false if no match, as the

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

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