Skip to content

Commit 42442ed

Browse files
committed
Fix Regexp#match for GC compaction
The test fails when RGENGC_CHECK_MODE is turned on: TestRegexp#test_match_under_gc_compact_stress: NoMethodError: undefined method `match' for nil test_regexp.rb:878:in `block in test_match_under_gc_compact_stress'
1 parent 37753f1 commit 42442ed

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

re.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,6 @@ rb_reg_prepare_re(VALUE re, VALUE str)
15811581
{
15821582
int r;
15831583
OnigErrorInfo einfo;
1584-
const char *pattern;
15851584
VALUE unescaped;
15861585
rb_encoding *fixed_enc = 0;
15871586
rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
@@ -1590,11 +1589,13 @@ rb_reg_prepare_re(VALUE re, VALUE str)
15901589
if (reg->enc == enc) return reg;
15911590

15921591
rb_reg_check(re);
1593-
pattern = RREGEXP_SRC_PTR(re);
1592+
1593+
VALUE src_str = RREGEXP_SRC(re);
1594+
const char *pattern = RSTRING_PTR(src_str);
15941595

15951596
onig_errmsg_buffer err = "";
15961597
unescaped = rb_reg_preprocess(
1597-
pattern, pattern + RREGEXP_SRC_LEN(re), enc,
1598+
pattern, pattern + RSTRING_LEN(src_str), enc,
15981599
&fixed_enc, err, 0);
15991600

16001601
if (NIL_P(unescaped)) {
@@ -1639,6 +1640,7 @@ rb_reg_prepare_re(VALUE re, VALUE str)
16391640
reg->timelimit = timelimit;
16401641

16411642
RB_GC_GUARD(unescaped);
1643+
RB_GC_GUARD(src_str);
16421644
return reg;
16431645
}
16441646

test/ruby/test_regexp.rb

+7
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,13 @@ def test_match
872872
$_ = nil; assert_nil(~/./)
873873
end
874874

875+
def test_match_under_gc_compact_stress
876+
EnvUtil.under_gc_compact_stress do
877+
m = /(?<foo>.)(?<n>[^aeiou])?(?<bar>.+)/.match("hoge\u3042")
878+
assert_equal("h", m.match(:foo))
879+
end
880+
end
881+
875882
def test_match_p
876883
/backref/ =~ 'backref'
877884
# must match here, but not in a separate method, e.g., assert_send,

0 commit comments

Comments
 (0)