Skip to content

Commit

Permalink
Detect detached TypedArray in indexOf.
Browse files Browse the repository at this point in the history
Summary:
`includes` et al. can detach a TypedArray in the `ToInteger` call
for the argument.
Check for this.

Reviewed By: dulinriley

Differential Revision: D22344490

fbshipit-source-id: 4f37735859bca8882226b9da8876154d66a09888
  • Loading branch information
avp authored and facebook-github-bot committed Jul 17, 2020
1 parent da086a4 commit 963e658
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/VM/JSLib/TypedArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@ typedArrayPrototypeIndexOf(void *ctx, Runtime *runtime, NativeArgs args) {
return ExecutionStatus::EXCEPTION;
}
fromIndex = res->getNumber();
if (LLVM_UNLIKELY(!self->attached(runtime))) {
// If the ToInteger call detached this TypedArray, raise a TypeError and
// don't continue.
return runtime->raiseTypeError("Detached the TypedArray in the callback");
}
}
// Negative zero case.
if (fromIndex == 0) {
Expand Down
9 changes: 9 additions & 0 deletions test/hermes/TypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,15 @@ cons.forEach(function(TypedArray) {
arr[1] = 50;
assert.equal(arr.indexOf(50), 0);
assert.equal(arr.lastIndexOf(50), 1);

assert.throws(function() {
arr.includes(0, {
valueOf() {
HermesInternal.detachArrayBuffer(arr.buffer);
return 0;
},
});
}, TypeError);
});
/// @}

Expand Down

0 comments on commit 963e658

Please sign in to comment.