ruby-changes:67540
From: Aaron <ko1@a...>
Date: Thu, 2 Sep 2021 01:23:00 +0900 (JST)
Subject: [ruby-changes:67540] cd4f5b1322 (master): Guard array when appending
https://git.ruby-lang.org/ruby.git/commit/?id=cd4f5b1322 From cd4f5b13228879d954fa97b6aa479c4a5ef4fb0a Mon Sep 17 00:00:00 2001 From: Aaron Patterson <tenderlove@r...> Date: Tue, 31 Aug 2021 16:58:29 -0700 Subject: 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] --- array.c | 1 + test/ruby/test_array.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/array.c b/array.c index 34fdc7b..50a61db 100644 --- a/array.c +++ b/array.c @@ -4845,6 +4845,7 @@ ary_append(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/array.c#L4845 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 59e1ad4..8de51c1 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -654,6 +654,12 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L654 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 -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/