Skip to content

Commit 474c5d2

Browse files
committed
LibJS: Prevent huge memory allocations for bigint left shift
1 parent ef81e57 commit 474c5d2

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

Libraries/LibJS/Runtime/ErrorTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
M(BigIntBadOperatorOtherType, "Cannot use {} operator with BigInt and other type") \
2222
M(BigIntFromNonIntegral, "Cannot convert non-integral number to BigInt") \
2323
M(BigIntInvalidValue, "Invalid value for BigInt: {}") \
24+
M(BigIntSizeExceeded, "Maximum BigInt size exceeded") \
2425
M(BindingNotInitialized, "Binding {} is not initialized") \
2526
M(BufferOutOfBounds, "{} contains a property which references a value at an index not contained within its buffer's bounds") \
2627
M(ByteLengthExceedsMaxByteLength, "ArrayBuffer byte length of {} exceeds the max byte length of {}") \

Libraries/LibJS/Runtime/Value.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,11 @@ ThrowCompletionOr<Value> left_shift(VM& vm, Value lhs, Value rhs)
15931593
return Value(lhs_i32 << shift_count);
15941594
}
15951595
if (both_bigint(lhs_numeric, rhs_numeric)) {
1596+
// AD-HOC: Prevent allocating huge amounts of memory.
1597+
auto rhs_bigint = rhs_numeric.as_bigint().big_integer().unsigned_value();
1598+
if (rhs_bigint.byte_length() > sizeof(u32))
1599+
return vm.throw_completion<RangeError>(ErrorType::BigIntSizeExceeded);
1600+
15961601
// 6.1.6.2.9 BigInt::leftShift ( x, y ), https://tc39.es/ecma262/#sec-numeric-types-bigint-leftShift
15971602
auto multiplier_divisor = Crypto::SignedBigInteger { Crypto::NumberTheory::Power(Crypto::UnsignedBigInteger(2), rhs_numeric.as_bigint().big_integer().unsigned_value()) };
15981603

0 commit comments

Comments
 (0)