aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2016-11-15 16:53:51 +0900
committerKazuki Yamaguchi <k@rhe.jp>2016-11-19 13:55:50 +0000
commite6720063c6503ba273674735703c5aa3591beef3 (patch)
tree4e675ee3f085397a3f9b1a6940240366ba95c5ca
parent104dd8c92fb2f029a1ae1a44377d552e29d729de (diff)
downloadruby-e6720063c6503ba273674735703c5aa3591beef3.tar.gz
re.c: make MatchData#hash and #== check the MatchData is initialized
Fix possible segfaults that can occur if they are called against an uninitialized MatchData. MatchData#hash now calls match_check() before dereferencing the String, and MatchData#== (#eql?) checks that RMatch::regexp is non-zero.
-rw-r--r--re.c5
-rw-r--r--test/ruby/test_regexp.rb1
2 files changed, 5 insertions, 1 deletions
diff --git a/re.c b/re.c
index 0536aa6..9373473 100644
--- a/re.c
+++ b/re.c
@@ -2948,8 +2948,10 @@ static VALUE
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));
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)
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);
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 4853c41..b0da247 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -941,6 +941,7 @@ class TestRegexp < Test::Unit::TestCase
assert_raise(TypeError) { MatchData.allocate.to_s }
assert_match(/^#<MatchData:.*>$/, MatchData.allocate.inspect)
assert_raise(TypeError) { MatchData.allocate.string }
+ assert_raise(TypeError) { MatchData.allocate.hash }
$~ = MatchData.allocate
assert_raise(TypeError) { $& }
assert_raise(TypeError) { $` }