Skip to content

Truly independent web browser

LibJS: Reject a sign with no digits in StringToBigInt

Shannon Booth931a7147dc69
diff --git a/Libraries/LibJS/Rust/src/runtime/value_conversions.rs b/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
index 22401c500e6..538a1e5534f 100644
--- a/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
+++ b/Libraries/LibJS/Rust/src/runtime/value_conversions.rs
@@ -225,11 +225,19 @@ fn parse_bigint_text(mut text: &[u16]) -> Option<BigIntParseResult<'_>> {
}
let mut is_negative = false;
+ let mut has_sign = false;
if text.first() == Some(&u16::from(b'-')) {
text = &text[1..];
is_negative = true;
+ has_sign = true;
} else if text.first() == Some(&u16::from(b'+')) {
text = &text[1..];
+ has_sign = true;
+ }
+
+ // NB: An empty StringIntegerLiteral is 0n, but a sign must be followed by a digit.
+ if has_sign && text.is_empty() {
+ return None;
}
if !text.iter().all(|code_unit| is_ascii_digit(*code_unit)) {
@@ -244,7 +252,6 @@ fn parse_bigint_text(mut text: &[u16]) -> Option<BigIntParseResult<'_>> {
}
/// 7.1.14 StringToBigInt ( str ), https://tc39.es/ecma262/#sec-stringtobigint
-/// This accepts a lone sign as 0n.
pub fn string_to_bigint(string: &[u16]) -> Option<BigInt> {
// 1. Let text be StringToCodePoints(str).
let text = trim_js_whitespace(string);
diff --git a/Tests/LibJS/Runtime/builtins/BigInt/BigInt.js b/Tests/LibJS/Runtime/builtins/BigInt/BigInt.js
index 7517ceffe41..584607b9929 100644
--- a/Tests/LibJS/Runtime/builtins/BigInt/BigInt.js
+++ b/Tests/LibJS/Runtime/builtins/BigInt/BigInt.js
@@ -139,4 +139,15 @@ describe("errors", () => {
}).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
});
});
+
+ test("sign without digits", () => {
+ ["-", "+", " - "].forEach(value => {
+ expect(() => {
+ BigInt(value);
+ }).toThrowWithMessage(SyntaxError, `Invalid value for BigInt: ${value}`);
+ });
+
+ expect("-" == 0n).toBeFalse();
+ expect("+" < 1n).toBeFalse();
+ });
});