From c711e404ef2470950c36187aeb9273aa0b589bc4 Mon Sep 17 00:00:00 2001 From: gorogoroumaru Date: Fri, 16 Oct 2020 17:00:07 +0900 Subject: [PATCH] Make boa::parse emit error on invalid input, not panic (#807) * make boa::parse emit error on invalid input, not panic * formatting * applied requested change * formatting * Update boa/src/syntax/lexer/number.rs Co-authored-by: Halid Odat * Update boa/src/syntax/lexer/number.rs Co-authored-by: Halid Odat * applied requested change Co-authored-by: Halid Odat --- boa/src/syntax/ast/op.rs | 2 +- boa/src/syntax/lexer/number.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/boa/src/syntax/ast/op.rs b/boa/src/syntax/ast/op.rs index 0d3c816dcd..ba49caff36 100644 --- a/boa/src/syntax/ast/op.rs +++ b/boa/src/syntax/ast/op.rs @@ -602,7 +602,7 @@ pub enum CompOp { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in In, - /// The `instanceop` operator returns `true` if the specified object is an instance of the + /// The `instanceof` operator returns `true` if the specified object is an instance of the /// right hand side object. /// /// Syntax: `obj instanceof Object` diff --git a/boa/src/syntax/lexer/number.rs b/boa/src/syntax/lexer/number.rs index 6e1c4581f8..deb4db0516 100644 --- a/boa/src/syntax/lexer/number.rs +++ b/boa/src/syntax/lexer/number.rs @@ -157,6 +157,16 @@ impl Tokenizer for NumberLiteral { // HexIntegerLiteral kind = NumericKind::Integer(16); + + // Checks if the next char after '0x' is a digit of that base. if not return an error. + if let Some(digit) = cursor.peek()? { + if !digit.is_digit(16) { + return Err(Error::syntax( + "expected hexadecimal digit after number base prefix", + cursor.pos(), + )); + } + } } 'o' | 'O' => { // Remove the initial '0' from buffer. @@ -165,6 +175,16 @@ impl Tokenizer for NumberLiteral { // OctalIntegerLiteral kind = NumericKind::Integer(8); + + // Checks if the next char after '0o' is a digit of that base. if not return an error. + if let Some(digit) = cursor.peek()? { + if !digit.is_digit(8) { + return Err(Error::syntax( + "expected hexadecimal digit after number base prefix", + cursor.pos(), + )); + } + } } 'b' | 'B' => { // Remove the initial '0' from buffer. @@ -173,6 +193,16 @@ impl Tokenizer for NumberLiteral { // BinaryIntegerLiteral kind = NumericKind::Integer(2); + + // Checks if the next char after '0b' is a digit of that base. if not return an error. + if let Some(digit) = cursor.peek()? { + if !digit.is_digit(2) { + return Err(Error::syntax( + "expected hexadecimal digit after number base prefix", + cursor.pos(), + )); + } + } } 'n' => { cursor.next_char()?.expect("n character vanished"); @@ -294,7 +324,7 @@ impl Tokenizer for NumberLiteral { let val = f64::from_str(&buf).expect("Failed to parse float after checks"); let int_val = val as i32; - // The truncated float should be identically to the non-truncated float for the conversion to be loss-less, + // The truncated float should be identically to the non-truncated float for the conversion to be loss-less, // any other different and the number must be stored as a rational. #[allow(clippy::float_cmp)] if (int_val as f64) == val {