Skip to content

Truly independent web browser

LibJS: Throw a RangeError for a BigInt.asIntN/asUintN result too large

Shannon Booth518c5074bb94
diff --git a/Libraries/LibJS/Rust/src/runtime/big_int_algorithms.rs b/Libraries/LibJS/Rust/src/runtime/big_int_algorithms.rs
index 64313514bf1..8add2fa777e 100644
--- a/Libraries/LibJS/Rust/src/runtime/big_int_algorithms.rs
+++ b/Libraries/LibJS/Rust/src/runtime/big_int_algorithms.rs
@@ -214,9 +214,9 @@ pub fn mod_power_of_two(value: &BigInt, power_of_two: u64) -> Result<BigInt, Num
return Ok(value.clone());
}
- // If the power of two overflows the int type, we don't have enough memory to compute it.
+ // AD-HOC: A power of two that overflows the int type is too large to compute, as for BigInt::leftShift.
if i32::try_from(power_of_two).is_err() {
- return Err(NumericOperationError::OutOfMemory);
+ return Err(NumericOperationError::BigIntSizeExceeded);
}
let magnitude_modulo = low_bits(value.magnitude(), power_of_two);
@@ -234,7 +234,7 @@ fn bitwise_not_fill_to_one_based_index(value: &BigUint, index: u64) -> Result<Bi
return Ok(BigUint::zero());
}
if i32::try_from(index).is_err() {
- return Err(NumericOperationError::OutOfMemory);
+ return Err(NumericOperationError::BigIntSizeExceeded);
}
debug_assert!(value.bits() <= index);
Ok((BigUint::one() << index) - 1u32 - value)
diff --git a/Libraries/LibJS/Rust/src/runtime/value_conversions.rs b/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
index a32847509b1..22401c500e6 100644
--- a/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
+++ b/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
@@ -34,8 +34,6 @@ pub enum NumericOperationError {
BigIntSizeExceeded,
/// A RangeError with ErrorType::NegativeExponent.
NegativeExponent,
- /// The InternalError that TRY_OR_THROW_OOM throws when an operation reports ENOMEM.
- OutOfMemory,
}
impl NumericOperationError {
@@ -46,7 +44,6 @@ impl NumericOperationError {
Self::BigIntFromNonIntegral => (ErrorKind::RangeError, ErrorType::BigIntFromNonIntegral),
Self::BigIntSizeExceeded => (ErrorKind::RangeError, ErrorType::BigIntSizeExceeded),
Self::NegativeExponent => (ErrorKind::RangeError, ErrorType::NegativeExponent),
- Self::OutOfMemory => (ErrorKind::InternalError, ErrorType::OutOfMemory),
}
}
diff --git a/Tests/LibJS/Runtime/builtins/BigInt/BigInt.asUintN.js b/Tests/LibJS/Runtime/builtins/BigInt/BigInt.asUintN.js
index 1d8252f0e96..a0816f201e6 100644
--- a/Tests/LibJS/Runtime/builtins/BigInt/BigInt.asUintN.js
+++ b/Tests/LibJS/Runtime/builtins/BigInt/BigInt.asUintN.js
@@ -88,6 +88,6 @@ describe("correct behavior", () => {
expect(BigInt.asUintN(INDEX_MAX, 2n)).toBe(2n);
expect(() => {
BigInt.asUintN(INDEX_MAX, -2n);
- }).toThrowWithMessage(InternalError, "Out of memory");
+ }).toThrowWithMessage(RangeError, "Maximum BigInt size exceeded");
});
});