summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--string.c2
-rw-r--r--test/ruby/test_string.rb9
2 files changed, 11 insertions, 0 deletions
diff --git a/string.c b/string.c
index e46af72def..712c963058 100644
--- a/string.c
+++ b/string.c
@@ -5816,6 +5816,8 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
TERM_FILL(&RSTRING_PTR(str)[len], TERM_LEN(str));
ENC_CODERANGE_SET(str, cr);
+ RB_GC_GUARD(match);
+
return str;
}
return Qnil;
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index ae392b2da6..333b610bc5 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -2081,6 +2081,15 @@ CODE
}
end
+ def test_sub_gc_compact_stress
+ EnvUtil.under_gc_compact_stress do
+ m = /&(?<foo>.*?);/.match(S("aaa &amp; yyy"))
+ assert_equal("amp", m["foo"])
+
+ assert_equal("aaa [amp] yyy", S("aaa &amp; yyy").sub(/&(?<foo>.*?);/, S('[\k<foo>]')))
+ end
+ end
+
def test_sub!
a = S("hello")
b = a.dup