Skip to content

Commit 1774cca

Browse files
authored
Fix array length check in lastIndexOf (#3789)
When lastIndexOf is executed the array length can change and in case of fast arrays the length should be re-checked. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.usz@partner.samsung.com
1 parent 589af6d commit 1774cca

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1775,9 +1775,11 @@ ecma_builtin_array_prototype_object_last_index_of (const ecma_value_t args[], /*
17751775
if (ecma_op_object_is_fast_array (obj_p))
17761776
{
17771777
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
1778+
// It is possible that the length changed due to the callback performed above.
1779+
uint32_t array_length = ext_obj_p->u.array.length;
17781780

17791781
if (ext_obj_p->u.array.u.hole_count < ECMA_FAST_ARRAY_HOLE_ONE
1780-
&& len != 0)
1782+
&& array_length > 0)
17811783
{
17821784
ecma_value_t *buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, obj_p->u1.property_list_cp);
17831785

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 array = [1, 2, 3];
16+
var idx_50 = array.lastIndexOf(50, {
17+
valueOf: function() {
18+
// Trigger removing elements from fast array.
19+
array.length = 0;
20+
}
21+
})
22+
23+
assert (idx_50 === -1);
24+
25+
var idx_51 = array.lastIndexOf(51, {
26+
valueOf: function() {
27+
array.push(51)
28+
return 10;
29+
}
30+
})
31+
32+
assert (idx_51 === -1);

0 commit comments

Comments
 (0)