summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2021-08-31 16:58:29 -0700
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-09-02 01:22:19 +0900
commitcd4f5b13228879d954fa97b6aa479c4a5ef4fb0a (patch)
tree8c381dcc4235579c63b4fd69a3bff2f84808329b
parent73b22b3ce9994b3e674de4d5016d2d9130b0e27d (diff)
Guard array when appending
This prevents early collection of the array. The GC doesn't see the array on the stack when Ruby is compiled with optimizations enabled [ruby-core:105099] [Bug #18140]
-rw-r--r--array.c1
-rw-r--r--test/ruby/test_array.rb6
2 files changed, 7 insertions, 0 deletions
diff --git a/array.c b/array.c
index 34fdc7b22a..50a61db876 100644
--- a/array.c
+++ b/array.c
@@ -4845,6 +4845,7 @@ ary_append(VALUE x, VALUE y)
if (n > 0) {
rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR_TRANSIENT(y), n);
}
+ RB_GC_GUARD(y);
return x;
}
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 59e1ad463c..8de51c159d 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -654,6 +654,12 @@ class TestArray < Test::Unit::TestCase
assert_raise(TypeError) { [0].concat(:foo) }
assert_raise(FrozenError) { [0].freeze.concat(:foo) }
+
+ a = @cls[nil]
+ def (x = Object.new).to_ary; Array.new(10) {nil} << :ok; end
+ EnvUtil.under_gc_stress {a.concat(x)}
+ GC.start
+ assert_equal(:ok, a.last)
end
def test_count