diff options
author | Peter Zhu <peter@peterzhu.ca> | 2023-12-24 19:22:13 -0500 |
---|---|---|
committer | Peter Zhu <peter@peterzhu.ca> | 2023-12-24 21:29:40 -0500 |
commit | 7002e776944ddfd362cea253d18d02bc250fe9f7 (patch) | |
tree | 1aa1b9479a6dc76251eac1cf2226a3af3f404683 | |
parent | e233730846fd2634208c3c568c49b76bf3b29a0b (diff) | |
download | ruby-7002e776944ddfd362cea253d18d02bc250fe9f7.tar.gz |
Fix Symbol#inspect for GC compaction
The test fails when RGENGC_CHECK_MODE is turned on:
1) Failure:
TestSymbol#test_inspect_under_gc_compact_stress [test/ruby/test_symbol.rb:123]:
<":testing"> expected but was
<":\x00\x00\x00\x00\x00\x00\x00">.
-rw-r--r-- | string.c | 7 | ||||
-rw-r--r-- | test/ruby/test_symbol.rb | 6 |
2 files changed, 12 insertions, 1 deletions
@@ -11630,10 +11630,15 @@ sym_inspect(VALUE sym) } else { rb_encoding *enc = STR_ENC_GET(str); - RSTRING_GETMEM(str, ptr, len); + + VALUE orig_str = str; + RSTRING_GETMEM(orig_str, ptr, len); + str = rb_enc_str_new(0, len + 1, enc); dest = RSTRING_PTR(str); memcpy(dest + 1, ptr, len); + + RB_GC_GUARD(orig_str); } dest[0] = ':'; return str; diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index 1d2a18d734..7f75ecd90e 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -118,6 +118,12 @@ class TestSymbol < Test::Unit::TestCase end end + def test_inspect_under_gc_compact_stress + EnvUtil.under_gc_compact_stress do + assert_inspect_evaled(':testing') + end + end + def test_name assert_equal("foo", :foo.name) assert_same(:foo.name, :foo.name) |