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

ruby-changes:44921

From: rhe <ko1@a...>
Date: Tue, 6 Dec 2016 15:14:21 +0900 (JST)
Subject: [ruby-changes:44921] rhe:r56994 (trunk): re.c: check that MatchData is initialized

rhe	2016-12-06 15:14:17 +0900 (Tue, 06 Dec 2016)

  New Revision: 56994

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

  Log:
    re.c: check that MatchData is initialized
    
    Follow r16757 ("* re.c: fix SEGV by Regexp.allocate.names,
    Match.allocate.names, etc.", 2008-06-02). Don't do null dereference if
    MatchData#hash or #== is called against an uninitialized instance.

  Modified files:
    trunk/re.c
    trunk/test/ruby/test_regexp.rb
Index: re.c
===================================================================
--- re.c	(revision 56993)
+++ re.c	(revision 56994)
@@ -2948,8 +2948,10 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/re.c#L2948
 match_hash(VALUE match)
 {
     const struct re_registers *regs;
-    st_index_t hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
+    st_index_t hashval;
 
+    match_check(match);
+    hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
     hashval = rb_hash_uint(hashval, reg_hash(RMATCH(match)->regexp));
     regs = RMATCH_REGS(match);
     hashval = rb_hash_uint(hashval, regs->num_regs);
@@ -2974,6 +2976,7 @@ match_equal(VALUE match1, VALUE match2) https://github.com/ruby/ruby/blob/trunk/re.c#L2976
     const struct re_registers *regs1, *regs2;
     if (match1 == match2) return Qtrue;
     if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
+    if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
     if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
     if (!rb_reg_equal(RMATCH(match1)->regexp, RMATCH(match2)->regexp)) return Qfalse;
     regs1 = RMATCH_REGS(match1);
Index: test/ruby/test_regexp.rb
===================================================================
--- test/ruby/test_regexp.rb	(revision 56993)
+++ test/ruby/test_regexp.rb	(revision 56994)
@@ -946,6 +946,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L946
     assert_raise(TypeError) { Regexp.allocate.names }
     assert_raise(TypeError) { Regexp.allocate.named_captures }
 
+    assert_raise(TypeError) { MatchData.allocate.hash }
     assert_raise(TypeError) { MatchData.allocate.regexp }
     assert_raise(TypeError) { MatchData.allocate.names }
     assert_raise(TypeError) { MatchData.allocate.size }

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

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