Skip to content

Commit 086c4eb

Browse files
authored
Fix array hole calculation for fast access mode arrays (#3061)
This patch fixes #3060. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 142f79c commit 086c4eb

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

jerry-core/ecma/operations/ecma-array-object.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ ecma_fast_array_set_property (ecma_object_t *object_p, /**< fast access mode arr
244244

245245
if (ecma_is_value_array_hole (values_p[index]))
246246
{
247-
ext_obj_p->u.array.hole_count--;
247+
ext_obj_p->u.array.hole_count = (uint8_t) JERRY_MAX (ext_obj_p->u.array.hole_count - 1, 0);
248248
}
249249
else
250250
{
@@ -269,21 +269,25 @@ ecma_fast_array_set_property (ecma_object_t *object_p, /**< fast access mode arr
269269
values_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, object_p->u1.property_list_cp);
270270
JERRY_ASSERT (ecma_is_value_array_hole (values_p[index]));
271271
ext_obj_p->u.array.length = new_length;
272+
ext_obj_p->u.array.hole_count = (uint8_t) JERRY_MIN (ext_obj_p->u.array.hole_count + new_holes,
273+
ECMA_FAST_ARRAY_MAX_HOLE_COUNT);
272274
}
273275
else
274276
{
275-
if ((new_holes + ext_obj_p->u.array.hole_count) > ECMA_FAST_ARRAY_MAX_HOLE_COUNT)
277+
JERRY_ASSERT (ext_obj_p->u.array.hole_count <= ECMA_FAST_ARRAY_MAX_HOLE_COUNT);
278+
279+
if (new_holes > (uint32_t) (ECMA_FAST_ARRAY_MAX_HOLE_COUNT - ext_obj_p->u.array.hole_count))
276280
{
277281
ecma_fast_array_convert_to_normal (object_p);
278282

279283
return false;
280284
}
281285

282286
values_p = ecma_fast_array_extend (object_p, new_length);
287+
ext_obj_p->u.array.hole_count = (uint8_t) (ext_obj_p->u.array.hole_count + new_holes);
283288
}
284289

285290
values_p[index] = ecma_copy_value_if_not_object (value);
286-
ext_obj_p->u.array.hole_count = (uint8_t) (ext_obj_p->u.array.hole_count + new_holes);
287291

288292
return true;
289293
} /* ecma_fast_array_set_property */
@@ -372,7 +376,11 @@ ecma_array_object_delete_property (ecma_object_t *object_p, /**< object */
372376
ecma_free_value_if_not_object (values_p[index]);
373377

374378
values_p[index] = ECMA_VALUE_ARRAY_HOLE;
375-
ext_obj_p->u.array.hole_count++;
379+
380+
if (++ext_obj_p->u.array.hole_count > ECMA_FAST_ARRAY_MAX_HOLE_COUNT)
381+
{
382+
ecma_fast_array_convert_to_normal (object_p);
383+
}
376384
} /* ecma_array_object_delete_property */
377385

378386
/**
@@ -416,7 +424,7 @@ ecma_delete_fast_array_properties (ecma_object_t *object_p, /**< fast access mod
416424
{
417425
if (ecma_is_value_array_hole (values_p[i]))
418426
{
419-
ext_obj_p->u.array.hole_count--;
427+
ext_obj_p->u.array.hole_count = (uint8_t) JERRY_MAX (ext_obj_p->u.array.hole_count - 1, 0);
420428
}
421429
else
422430
{
@@ -469,7 +477,9 @@ ecma_fast_array_set_length (ecma_object_t *object_p, /**< fast access mode array
469477

470478
uint32_t new_holes = new_length - old_length - 1;
471479

472-
if ((new_holes + ext_obj_p->u.array.hole_count) > ECMA_FAST_ARRAY_MAX_HOLE_COUNT)
480+
JERRY_ASSERT (ext_obj_p->u.array.hole_count <= ECMA_FAST_ARRAY_MAX_HOLE_COUNT);
481+
482+
if (new_holes > (uint32_t) (ECMA_FAST_ARRAY_MAX_HOLE_COUNT - ext_obj_p->u.array.hole_count))
473483
{
474484
ecma_fast_array_convert_to_normal (object_p);
475485
return;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
var arr = [];
16+
arr.length = 10;
17+
arr.splice(0, 17);
18+
arr.length = 4294967294;
19+
arr.splice(1, 1, 1);
20+
21+
assert(arr.length === 4294967294);
22+
assert(arr[0] === undefined);
23+
assert(arr[1] === 1);
24+
assert(arr[2] === undefined);

0 commit comments

Comments
 (0)