Skip to content

Commit 9da491d

Browse files
committed
Fix Array index normalize helper when index is large.
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
1 parent 63083b3 commit 9da491d

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,14 @@ ecma_builtin_helper_array_index_normalize (ecma_number_t index, /**< index */
299299
}
300300
else
301301
{
302-
const int32_t int_index = ecma_number_to_int32 (index);
303-
304-
if (int_index < 0)
302+
if (ecma_number_is_negative (index))
305303
{
306-
const uint32_t uint_index = (uint32_t) - int_index;
304+
const uint32_t uint_index = ecma_number_to_uint32 (ecma_number_negate (index));
307305
norm_index = uint_index > length ? 0 : length - uint_index;
308306
}
309307
else
310308
{
311-
norm_index = (uint32_t) int_index;
309+
norm_index = ecma_number_to_uint32 (index);
312310

313311
if (norm_index > length)
314312
{

tests/jerry/array-prototype-slice.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ assert (array7[3] == -127);
5858

5959
assert (array8.length == 0);
6060

61+
var array = [];
62+
array[4294967293] = "foo";
63+
array.length = 4294967295;
64+
var result = array.slice(4294967293, -1)
65+
assert(result.length === 1)
66+
assert(result[0] === "foo")
67+
68+
array[0] = "bar";
69+
var result = array.slice(-4294967295, -4294967294)
70+
assert(result.length === 1)
71+
assert(result[0] === "bar")
72+
6173
// Checking behavior when unable to get length
6274
var obj = { slice : Array.prototype.slice };
6375
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });

tests/jerry/array-prototype-splice.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ assert (array[2] == -127);
119119
assert (array[3] == "sunshine");
120120
assert (array9.length == 0);
121121

122+
var array = [];
123+
array[4294967294] = "foo";
124+
var result = array.splice(4294967294, 1, "x")
125+
assert(result.length === 1)
126+
assert(result[0] === "foo")
127+
assert(array[4294967294] === "x")
128+
129+
array[0] = "bar";
130+
var result = array.splice(-4294967295, 1, "y");
131+
assert(result.length === 1)
132+
assert(result[0] === "bar")
133+
assert(array[0] === "y")
134+
122135
// Checking behavior when unable to get length
123136
var obj = {splice : Array.prototype.splice};
124137
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });

0 commit comments

Comments
 (0)