From ec8e5f5aa64e2a54cf1e303f2b012c98e8d521ba Mon Sep 17 00:00:00 2001 From: Luke Gruber Date: Sat, 22 Jun 2019 18:37:08 -0400 Subject: [PATCH] array.c: always check frozenness in Array#unshift. Fixes [Bug #15952] Closes: https://github.com/ruby/ruby/pull/2251 --- array.c | 3 ++- test/ruby/test_array.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/array.c b/array.c index 8501074ebe..2d68b608c0 100644 --- a/array.c +++ b/array.c @@ -1373,6 +1373,8 @@ ary_ensure_room_for_unshift(VALUE ary, int argc) rb_raise(rb_eIndexError, "index %ld too big", new_len); } + rb_ary_modify(ary); + if (ARY_SHARED_P(ary)) { VALUE shared = ARY_SHARED(ary); capa = RARRAY_LEN(shared); @@ -1383,7 +1385,6 @@ ary_ensure_room_for_unshift(VALUE ary, int argc) } } - rb_ary_modify(ary); capa = ARY_CAPA(ary); if (capa - (capa >> 6) <= new_len) { ary_double_capa(ary, new_len); diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 88d61b79e0..b1778d191c 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1958,6 +1958,17 @@ class TestArray < Test::Unit::TestCase assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2])) end + def test_unshift_frozen + bug15952 = '[Bug #15952]' + assert_raise(FrozenError, bug15952) do + a = [1] * 100 + b = a[4..-1] + a.replace([1]) + b.freeze + b.unshift("a") + end + end + def test_OR # '|' assert_equal(@cls[], @cls[] | @cls[]) assert_equal(@cls[1], @cls[1] | @cls[])