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

array.c: always check frozenness in Array#unshift. Fixes [Bug #15952]

Closes: https://github.com/ruby/ruby/pull/2251
This commit is contained in:
Luke Gruber 2019-06-22 18:37:08 -04:00 committed by Nobuyoshi Nakada
parent 3840791b7e
commit ec8e5f5aa6
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 13 additions and 1 deletions

View file

@ -1373,6 +1373,8 @@ ary_ensure_room_for_unshift(VALUE ary, int argc)
rb_raise(rb_eIndexError, "index %ld too big", new_len); rb_raise(rb_eIndexError, "index %ld too big", new_len);
} }
rb_ary_modify(ary);
if (ARY_SHARED_P(ary)) { if (ARY_SHARED_P(ary)) {
VALUE shared = ARY_SHARED(ary); VALUE shared = ARY_SHARED(ary);
capa = RARRAY_LEN(shared); 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); capa = ARY_CAPA(ary);
if (capa - (capa >> 6) <= new_len) { if (capa - (capa >> 6) <= new_len) {
ary_double_capa(ary, new_len); ary_double_capa(ary, new_len);

View file

@ -1958,6 +1958,17 @@ class TestArray < Test::Unit::TestCase
assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2])) assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2]))
end 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 # '|' def test_OR # '|'
assert_equal(@cls[], @cls[] | @cls[]) assert_equal(@cls[], @cls[] | @cls[])
assert_equal(@cls[1], @cls[1] | @cls[]) assert_equal(@cls[1], @cls[1] | @cls[])