Skip to content

Commit 08da8bc

Browse files
Peter Markirerobika
authored andcommitted
Add length check to ecma_builtin_array_prototype_object_slice (#3481)
Fixes #3479 JerryScript-DCO-1.0-Signed-off-by: Peter Marki marpeter@inf.u-szeged.hu
1 parent dc3de8d commit 08da8bc

3 files changed

Lines changed: 105 additions & 8 deletions

File tree

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,9 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
828828
JERRY_ASSERT (start <= len && end <= len);
829829

830830
bool use_fast_path = ecma_op_object_is_fast_array (obj_p);
831+
uint32_t copied_length = (end > start) ? end - start : 0;
831832
#if ENABLED (JERRY_ES2015)
832-
ecma_value_t new_array = ecma_op_array_species_create (obj_p, 0);
833+
ecma_value_t new_array = ecma_op_array_species_create (obj_p, copied_length);
833834

834835
if (ECMA_IS_VALUE_ERROR (new_array))
835836
{
@@ -846,25 +847,45 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
846847
/* 9. */
847848
uint32_t n = 0;
848849

849-
if (use_fast_path)
850+
if (use_fast_path && copied_length > 0)
850851
{
851852
ecma_extended_object_t *ext_from_obj_p = (ecma_extended_object_t *) obj_p;
852853

853-
if (ext_from_obj_p->u.array.u.hole_count < ECMA_FAST_ARRAY_HOLE_ONE
854-
&& len != 0
855-
&& start < end)
854+
if (ext_from_obj_p->u.array.u.hole_count < ECMA_FAST_ARRAY_HOLE_ONE)
856855
{
857-
uint32_t length = end - start;
858856
ecma_extended_object_t *ext_to_obj_p = (ecma_extended_object_t *) new_array_p;
859-
ecma_value_t *to_buffer_p = ecma_fast_array_extend (new_array_p, length);
857+
858+
#if ENABLED (JERRY_ES2015)
859+
uint32_t target_length = ext_to_obj_p->u.array.length;
860+
ecma_value_t *to_buffer_p;
861+
if (copied_length == target_length)
862+
{
863+
to_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, new_array_p->u1.property_list_cp);
864+
}
865+
else if (copied_length > target_length)
866+
{
867+
to_buffer_p = ecma_fast_array_extend (new_array_p, copied_length);
868+
}
869+
else
870+
{
871+
ecma_delete_fast_array_properties (new_array_p, copied_length);
872+
to_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, new_array_p->u1.property_list_cp);
873+
}
874+
#else /* !ENABLED (JERRY_ES2015) */
875+
ecma_value_t *to_buffer_p = ecma_fast_array_extend (new_array_p, copied_length);
876+
#endif /* ENABLED (JERRY_ES2015) */
877+
860878
ecma_value_t *from_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, obj_p->u1.property_list_cp);
861879

862880
for (uint32_t k = start; k < end; k++, n++)
863881
{
882+
#if ENABLED (JERRY_ES2015)
883+
ecma_free_value_if_not_object (to_buffer_p[n]);
884+
#endif /* ENABLED (JERRY_ES2015) */
864885
to_buffer_p[n] = ecma_copy_value_if_not_object (from_buffer_p[k]);
865886
}
866887

867-
ext_to_obj_p->u.array.u.hole_count -= length * ECMA_FAST_ARRAY_HOLE_ONE;
888+
ext_to_obj_p->u.array.u.hole_count &= ECMA_FAST_ARRAY_HOLE_ONE - 1;
868889

869890
return new_array;
870891
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Constructor creates longer array than expected.
16+
class LongArray extends Array {
17+
constructor(len) {
18+
super (42);
19+
this.fill ("foo");
20+
}
21+
}
22+
23+
var a = new LongArray (5);
24+
a.length = 5;
25+
var sliced = a.slice ();
26+
assert (sliced.length == 5);
27+
assert (JSON.stringify (sliced) == '["foo","foo","foo","foo","foo"]')
28+
29+
// Constructor creates shorter array than expected.
30+
class ShortArray extends Array {
31+
constructor(len) {
32+
super (2);
33+
this.fill ("bar");
34+
}
35+
}
36+
37+
var b = new ShortArray (8);
38+
b.length = 8;
39+
b.fill ("asd", 2);
40+
var sliced2 = b.slice ();
41+
assert (sliced2.length == 8);
42+
assert (JSON.stringify (sliced2) == '["bar","bar","asd","asd","asd","asd","asd","asd"]');
43+
44+
// Constructor creates array of the expected size.
45+
class ExactArray extends Array {
46+
constructor(len) {
47+
super (len);
48+
this.fill ("baz");
49+
}
50+
}
51+
52+
var c = new ExactArray (5);
53+
var sliced3 = c.slice();
54+
assert (sliced3.length == 5);
55+
assert (JSON.stringify (sliced3) == '["baz","baz","baz","baz","baz"]');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
class MyObservedArray extends Array {
16+
constructor() {
17+
super('"use strict"; var x = "\\411";')
18+
} [Symbol]() {}
19+
}
20+
21+
new MyObservedArray().slice()

0 commit comments

Comments
 (0)