diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-12-07 08:11:05 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-12-07 16:43:19 +0000 |
commit | d37d6b3479aece9be1cb9fc5492e1c8e0f6a4cb8 (patch) | |
tree | ae0046458948c8b05d4dfa6a49ed1e05bf1ff8e9 /Userland/Libraries/LibJS/Tests | |
parent | 8f46cb83c7753d92654c6444ce2a20798d23c509 (diff) | |
download | serenity-d37d6b3479aece9be1cb9fc5492e1c8e0f6a4cb8.zip |
LibJS: Protect CanonicalIndex against double-to-integer overflow
Explicitly disallow constructing a CanonicalIndex from a floating point
type without going through a factory method that will throw when the
provided index cannot fit in a u32.
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.with.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.with.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.with.js index 9668e30a03..8c501331f9 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.with.js +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.with.js @@ -12,6 +12,44 @@ const TYPED_ARRAYS = [ const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; +describe("errors", () => { + test("index out of range", () => { + TYPED_ARRAYS.forEach(T => { + const array = new T([1, 2, 3]); + + expect(() => { + array.with(3, 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: 3"); + + expect(() => { + array.with(-4, 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: -1"); + }); + }); + + test("invalid index", () => { + TYPED_ARRAYS.forEach(T => { + const array = new T([1, 2, 3]); + + expect(() => { + array.with(2 ** 53, 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: 9007199254740992"); + + expect(() => { + array.with(-(2 ** 53), 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: -9007199254740989"); + + expect(() => { + array.with(Infinity, 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: inf"); + + expect(() => { + array.with(-Infinity, 10); + }).toThrowWithMessage(RangeError, "Invalid integer index: -inf"); + }); + }); +}); + describe("normal behavior", () => { test("length is 2", () => { TYPED_ARRAYS.forEach(T => { |