diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c index 61b56127bd..3dd02dbd5c 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c @@ -2178,6 +2178,9 @@ ecma_builtin_array_prototype_fill (ecma_value_t value, /**< value */ if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE) { + /* The array may have been shrunk by a valueOf call, so do not write past its current length. */ + final = JERRY_MIN (final, ext_obj_p->u.array.length); + if (JERRY_UNLIKELY (obj_p->u1.property_list_cp == JMEM_CP_NULL)) { ecma_ref_object (obj_p); @@ -2374,8 +2377,11 @@ ecma_builtin_array_prototype_object_copy_within (const ecma_value_t args[], /**< ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p; const uint32_t actual_length = ext_obj_p->u.array.length; + /* The array may have been resized by a valueOf call, so recheck both the source and the target range. */ + const ecma_length_t last_offset = forward ? count - 1 : 0; + if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE - && ((forward && (target + count - 1 < actual_length)) || (!forward && (target < actual_length)))) + && start + last_offset < actual_length && target + last_offset < actual_length) { if (obj_p->u1.property_list_cp != JMEM_CP_NULL) { diff --git a/tests/jerry/regression-test-issue-5282.js b/tests/jerry/regression-test-issue-5282.js new file mode 100644 index 0000000000..6a72c4b553 --- /dev/null +++ b/tests/jerry/regression-test-issue-5282.js @@ -0,0 +1,35 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var N = 100; +var arr = new Array(N); +for (var i = 0; i < N; i++) arr[i] = 0xAA00 + i; +arr.copyWithin(0, 50, { + valueOf: function() { + arr.length = 60; + return N; + } +}); +assert(arr.length === 60); +for (var i = 0; i < 10; i++) assert(arr[i] === 0xAA00 + 50 + i); +for (var i = 10; i < 50; i++) assert(arr[i] === undefined); +for (var i = 50; i < 60; i++) assert(arr[i] === 0xAA00 + i); + +/* backward copy */ +var b = new Array(N); +for (var i = 0; i < N; i++) b[i] = i; +b.copyWithin(40, 30, { valueOf: function () { b.length = 60; return 70; } }); +assert(b.length === 70); +assert(b[39] === 39); +for (var i = 40; i < 70; i++) assert(b[i] === i - 10); diff --git a/tests/jerry/regression-test-issue-5283.js b/tests/jerry/regression-test-issue-5283.js new file mode 100644 index 0000000000..00fc61ec0a --- /dev/null +++ b/tests/jerry/regression-test-issue-5283.js @@ -0,0 +1,28 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +var victim = new Array(100); +for (var i = 0; i < 100; i++) victim[i] = 0; +var adjacent; +victim.fill(0x41414141, { + valueOf: function() { + victim.length = 4; + adjacent = new Array(90); + for (var i = 0; i < 90; i++) adjacent[i] = 0xBBBB0000 + i; + return 0; + } +}); +for (var i = 0; i < 90; i++) assert(adjacent[i] === 0xBBBB0000 + i); +assert(victim.length === 4); +for (var i = 0; i < 4; i++) assert(victim[i] === 0x41414141);