1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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]
This commit is contained in:
Aaron Patterson 2021-08-31 16:58:29 -07:00 committed by Nobuyoshi Nakada
parent 73b22b3ce9
commit cd4f5b1322
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
2 changed files with 7 additions and 0 deletions

View file

@ -4845,6 +4845,7 @@ ary_append(VALUE x, VALUE y)
if (n > 0) { if (n > 0) {
rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR_TRANSIENT(y), n); rb_ary_splice(x, RARRAY_LEN(x), 0, RARRAY_CONST_PTR_TRANSIENT(y), n);
} }
RB_GC_GUARD(y);
return x; return x;
} }

View file

@ -654,6 +654,12 @@ class TestArray < Test::Unit::TestCase
assert_raise(TypeError) { [0].concat(:foo) } assert_raise(TypeError) { [0].concat(:foo) }
assert_raise(FrozenError) { [0].freeze.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 end
def test_count def test_count