diff --git a/Libraries/LibJS/Runtime/BigIntConstructor.cpp b/Libraries/LibJS/Runtime/BigIntConstructor.cpp index a101b8878fb..07f81a009e1 100644 --- a/Libraries/LibJS/Runtime/BigIntConstructor.cpp +++ b/Libraries/LibJS/Runtime/BigIntConstructor.cpp @@ -75,7 +75,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_int_n) // 3. Let mod be ℝ(bigint) modulo 2^bits. // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to // drop the most significant bits. - auto bits_shift_left = BIGINT_ONE.shift_left(bits); + auto bits_shift_left = TRY_OR_THROW_OOM(vm, BIGINT_ONE.try_shift_left(bits)); auto mod = modulo(bigint->big_integer(), bits_shift_left); // 4. If mod ≥ 2^(bits-1), return ℤ(mod - 2^bits); otherwise, return ℤ(mod). @@ -101,7 +101,7 @@ JS_DEFINE_NATIVE_FUNCTION(BigIntConstructor::as_uint_n) // 3. Return the BigInt value that represents ℝ(bigint) modulo 2bits. // FIXME: For large values of `bits`, this can likely be improved with a SignedBigInteger API to // drop the most significant bits. - return BigInt::create(vm, modulo(bigint->big_integer(), BIGINT_ONE.shift_left(bits))); + return BigInt::create(vm, modulo(bigint->big_integer(), TRY_OR_THROW_OOM(vm, BIGINT_ONE.try_shift_left(bits)))); } } diff --git a/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js index e448a7c72a8..4bc14a14b0b 100644 --- a/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js +++ b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asIntN.js @@ -22,6 +22,12 @@ describe("errors", () => { BigInt.asIntN(1, "foo"); }).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo"); }); + + test("large allocation", () => { + expect(() => { + BigInt.asIntN(0x4000000000000, 1n); + }).toThrowWithMessage(InternalError, "Out of memory"); + }); }); describe("correct behavior", () => { diff --git a/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js index 81fca8bdeec..059d575f557 100644 --- a/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js +++ b/Libraries/LibJS/Tests/builtins/BigInt/BigInt.asUintN.js @@ -22,6 +22,12 @@ describe("errors", () => { BigInt.asUintN(1, "foo"); }).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo"); }); + + test("large allocation", () => { + expect(() => { + BigInt.asUintN(0x4000000000000, 1n); + }).toThrowWithMessage(InternalError, "Out of memory"); + }); }); describe("correct behavior", () => {