Skip to content

Commit b8a9113

Browse files
bmeurerCommit Bot
authored andcommitted
[builtins] Fix out-of-bounds in Array#lastIndexOf().
The fast-path in the `ArrayPrototypeLastIndexOf` torque implementation didn't check that the `fromIndex` is within the bounds of the JSArray _AFTER_ the call to ToInteger, which can have arbitrary side-effects, i.e. it can change the length of the array. [email protected] Bug: chromium:898785 Change-Id: I7ef84143ec8c33148f6e9d451bd52769d5074fb4 Reviewed-on: https://chromium-review.googlesource.com/c/1314329 Reviewed-by: Yang Guo <[email protected]> Commit-Queue: Benedikt Meurer <[email protected]> Cr-Commit-Position: refs/heads/master@{#57204}
1 parent dffaff7 commit b8a9113

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/builtins/array-lastindexof.tq

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ module array {
2525
}
2626

2727
macro FastArrayLastIndexOf<Elements: type>(
28-
context: Context, array: JSArray, length: Smi, from: Smi,
29-
searchElement: Object): Smi {
28+
context: Context, array: JSArray, from: Smi, searchElement: Object): Smi {
3029
const elements: FixedArrayBase = array.elements;
3130
let k: Smi = from;
31+
32+
// Bug(898785): Due to side-effects in the evaluation of `fromIndex`
33+
// the {from} can be out-of-bounds here, so we need to clamp {k} to
34+
// the {elements} length. We might be reading holes / hole NaNs still
35+
// due to that, but those will be ignored below.
36+
if (k >= elements.length) {
37+
k = elements.length - 1;
38+
}
39+
3240
while (k >= 0) {
3341
try {
3442
const element: Object = LoadWithHoleCheck<Elements>(elements, k)
@@ -83,11 +91,11 @@ module array {
8391
const kind: ElementsKind = array.map.elements_kind;
8492
if (IsFastSmiOrTaggedElementsKind(kind)) {
8593
return FastArrayLastIndexOf<FixedArray>(
86-
context, array, length, fromSmi, searchElement);
94+
context, array, fromSmi, searchElement);
8795
}
8896
assert(IsDoubleElementsKind(kind));
8997
return FastArrayLastIndexOf<FixedDoubleArray>(
90-
context, array, length, fromSmi, searchElement);
98+
context, array, fromSmi, searchElement);
9199
}
92100

93101
transitioning macro GenericArrayLastIndexOf(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2018 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flags: --allow-natives-syntax
6+
7+
var a = [0, 1];
8+
var o = { [Symbol.toPrimitive]() { a.length = 1; return 2; } };
9+
10+
a.push(2);
11+
a.lastIndexOf(5, o);

0 commit comments

Comments
 (0)