Skip to content

Commit adee045

Browse files
committed
Bug 1965844 - Part 4: Correctly update search index for resized typed array in lastIndexOf. r=jandem
Test case in <tc39/test262#4477>. Differential Revision: https://phabricator.services.mozilla.com/D248929
1 parent 979a6c8 commit adee045

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

js/src/vm/TypedArrayObject.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,16 +2529,6 @@ static bool TypedArray_lastIndexOf(JSContext* cx, const CallArgs& args) {
25292529
return false;
25302530
}
25312531

2532-
// Reacquire the length because side-effects may have detached or resized
2533-
// the array buffer.
2534-
len = std::min(len, tarray->length().valueOr(0));
2535-
2536-
// Return early if the new length is zero.
2537-
if (len == 0) {
2538-
args.rval().setInt32(-1);
2539-
return true;
2540-
}
2541-
25422532
// Steps 6-8.
25432533
if (fromIndex >= 0) {
25442534
k = size_t(std::min(fromIndex, double(len - 1)));
@@ -2550,6 +2540,24 @@ static bool TypedArray_lastIndexOf(JSContext* cx, const CallArgs& args) {
25502540
}
25512541
k = size_t(d);
25522542
}
2543+
MOZ_ASSERT(k < len);
2544+
2545+
// Reacquire the length because side-effects may have detached or resized
2546+
// the array buffer.
2547+
size_t currentLength = tarray->length().valueOr(0);
2548+
2549+
// Restrict the search index and length if the new length is smaller.
2550+
if (currentLength < len) {
2551+
// Return early if the new length is zero.
2552+
if (currentLength == 0) {
2553+
args.rval().setInt32(-1);
2554+
return true;
2555+
}
2556+
2557+
// Otherwise just restrict |k| and |len| to the current length.
2558+
k = std::min(k, currentLength - 1);
2559+
len = currentLength;
2560+
}
25532561
}
25542562
MOZ_ASSERT(k < len);
25552563

0 commit comments

Comments
 (0)