Skip to content

Commit 4912e3b

Browse files
authored
Avoid buffer-overflow in Array.slice when using fast arrays (#4797)
In the Array.slice method when the engine uses fast arrays the "end" value was not updated if the input array's length changed. This can occur when the start/end index normalization executes a method and the length is changed forcefully. This leads to a buffer-overflow as the element copy reads too much data from the input array. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent b52c114 commit 4912e3b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,13 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
872872
return ecma_make_object_value (new_array_p);
873873
}
874874

875+
/* Source array's length could be changed during the start/end normalization.
876+
* If the "end" value is greater than the current length, clamp the value to avoid buffer-overflow. */
877+
if (ext_from_obj_p->u.array.length < end)
878+
{
879+
end = ext_from_obj_p->u.array.length;
880+
}
881+
875882
ecma_extended_object_t *ext_to_obj_p = (ecma_extended_object_t *) new_array_p;
876883

877884
#if JERRY_ESNEXT
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
16+
var fast_array = [];
17+
for (var i = 0; i < 1000; i++) {
18+
fast_array.push(i);
19+
}
20+
21+
var result_array = fast_array.slice(0, {valueOf: function() { fast_array.length = '3'; return 1000; }});
22+
23+
assert(result_array.length === 1000);
24+
25+
assert(result_array[0] === 0);
26+
assert(result_array[1] === 1);
27+
assert(result_array[2] === 2);
28+
29+
for (var i = 3; i < 1000; i++) {
30+
assert(typeof(result_array[i]) === "undefined");
31+
}

0 commit comments

Comments
 (0)