10000 Fix Regexp#to_s for GC compaction · github/ruby@fadda88 · GitHub
[go: up one dir, main page]

Skip to content

Commit fadda88

Browse files
committed
Fix Regexp#to_s for GC compaction
The test fails when RGENGC_CHECK_MODE is turned on: TestRegexp#test_to_s_under_gc_compact_stress = 13.46 s 1) Failure: TestRegexp#test_to_s_under_gc_compact_stress [test/ruby/test_regexp.rb:81]: <"(?-mix:abcd\u3042)"> expected but was <"(?-mix:\u5C78\u3030\u5C78\u3030\u5C78\u3030\u5C78\u3030\u5C78\u3030)">.
1 parent 688a131 commit fadda88

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

re.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,6 @@ rb_reg_str_with_term(VALUE re, int term)
565565
{
566566
int options, opt;
567567
const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
568-
long len;
569-
const UChar* ptr;
570568
VALUE str = rb_str_buf_new2("(?");
571569
char optbuf[OPTBUF_SIZE + 1]; /* for '-' */
572570
rb_encoding *enc = rb_enc_get(re);
@@ -575,8 +573,9 @@ rb_reg_str_with_term(VALUE re, int term)
575573

576574
rb_enc_copy(str, re);
577575
options = RREGEXP_PTR(re)->options;
578-
ptr = (UChar*)RREGEXP_SRC_PTR(re);
579-
len = RREGEXP_SRC_LEN(re);
576+
VALUE src_str = RREGEXP_SRC(re);
577+
const UChar *ptr = (UChar *)RSTRING_PTR(src_str);
578+
long len = RSTRING_LEN(src_str);
580579
again:
581580
if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
582581
int err = 1;
@@ -666,6 +665,8 @@ rb_reg_str_with_term(VALUE re, int term)
666665
}
667666
rb_enc_copy(str, re);
668667

668+
RB_GC_GUARD(src_str);
669+
669670
return str;
670671
}
671672

test/ruby/test_regexp.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ def test_to_s
7272
end
7373
end
7474

75+
def test_to_s_under_gc_compact_stress
76+
EnvUtil.under_gc_compact_stress do
77+
str = "abcd\u3042"
78+
[:UTF_16BE, :UTF_16LE, :UTF_32BE, :UTF_32LE].each do |es|
79+
enc = Encoding.const_get(es)
80+
rs = Regexp.new(str.encode(enc)).to_s
81+
assert_equal("(?-mix:abcd\u3042)".encode(enc), rs)
82+
assert_equal(enc, rs.encoding)
83+
end
84+
end
85+
end
86+
7587
def test_to_s_extended_subexp
7688
re = /#\g#{"\n"}/x
7789
re = /#{re}/

0 commit comments

Comments
 (0)
0