Skip to content

Commit

Permalink
Make boa::parse emit error on invalid input, not panic (#807)
Browse files Browse the repository at this point in the history
* 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 <halidodat@gmail.com>

* Update boa/src/syntax/lexer/number.rs

Co-authored-by: Halid Odat <halidodat@gmail.com>

* applied requested change

Co-authored-by: Halid Odat <halidodat@gmail.com>
  • Loading branch information
gorogoroumaru and HalidOdat committed Oct 16, 2020
1 parent 0357a8e commit c711e40
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion boa/src/syntax/ast/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
32 changes: 31 additions & 1 deletion boa/src/syntax/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ impl<R> Tokenizer<R> 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.
Expand All @@ -165,6 +175,16 @@ impl<R> Tokenizer<R> 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.
Expand All @@ -173,6 +193,16 @@ impl<R> Tokenizer<R> 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");
Expand Down Expand Up @@ -294,7 +324,7 @@ impl<R> Tokenizer<R> 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 {
Expand Down

0 comments on commit c711e40

Please sign in to comment.