Skip to content

Commit

Permalink
Fixed more Lexer Panics (#244)
Browse files Browse the repository at this point in the history
* Fixed more Lexer Panics
  • Loading branch information
adumbidiot authored Feb 20, 2020
1 parent edab5ca commit fd616c8
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions boa/src/syntax/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{

macro_rules! vop {
($this:ident, $assign_op:expr, $op:expr) => ({
let preview = $this.preview_next().expect("Could not preview next value");
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
match preview {
'=' => {
$this.next();
Expand All @@ -25,7 +25,7 @@ macro_rules! vop {
}
});
($this:ident, $assign_op:expr, $op:expr, {$($case:pat => $block:expr), +}) => ({
let preview = $this.preview_next().expect("Could not preview next value");
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
match preview {
'=' => {
$this.next();
Expand All @@ -39,7 +39,7 @@ macro_rules! vop {
}
});
($this:ident, $op:expr, {$($case:pat => $block:expr),+}) => {
let preview = $this.preview_next().expect("Could not preview next value");
let preview = $this.preview_next().ok_or_else(|| LexerError::new("Could not preview next value"))?;
match preview {
$($case => {
$this.next()?;
Expand Down Expand Up @@ -248,6 +248,9 @@ impl<'a> Lexer<'a> {
'"' | '\'' => {
let mut buf = String::new();
loop {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated String"));
}
match self.next() {
'\'' if ch == '\'' => {
break;
Expand All @@ -256,6 +259,9 @@ impl<'a> Lexer<'a> {
break;
}
'\\' => {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated String"));
}
let escape = self.next();
if escape != '\n' {
let escaped_ch = match escape {
Expand All @@ -268,6 +274,9 @@ impl<'a> Lexer<'a> {
'x' => {
let mut nums = String::with_capacity(2);
for _ in 0_u8..2 {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated String"));
}
nums.push(self.next());
}
self.column_number += 2;
Expand Down Expand Up @@ -302,6 +311,9 @@ impl<'a> Lexer<'a> {
};
let c = from_u32(as_num).ok_or_else(|| LexerError::new("Invalid Unicode escape sequence"))?;

if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated String"));
}
self.next(); // '}'
self.column_number +=
(s.len() as u64).wrapping_add(3);
Expand Down Expand Up @@ -395,11 +407,11 @@ impl<'a> Lexer<'a> {
}
}
if gone_decimal {
u64::from_str(&buf).expect("Could not convert value to u64")
u64::from_str(&buf).map_err(|_e| LexerError::new("Could not convert value to u64"))?
} else if buf.is_empty() {
0
} else {
u64::from_str_radix(&buf, 8).expect("Could not convert value to u64")
u64::from_str_radix(&buf, 8).map_err(|_e| LexerError::new("Could not convert value to u64"))?
}
}
Some(_) => {
Expand Down Expand Up @@ -531,6 +543,9 @@ impl<'a> Lexer<'a> {
'*' => {
let mut buf = String::new();
loop {
if self.preview_next().is_none() {
return Err(LexerError::new("Unterminated Multiline Comment"));
}
match self.next() {
'*' => {
if self.next_is('/') {
Expand Down Expand Up @@ -565,6 +580,9 @@ impl<'a> Lexer<'a> {
// escape sequence
Some('\\') => {
body.push('\\');
if self.preview_next().is_none() {
break;
}
match self.next() {
// newline not allowed in regex literal
'\n' | '\r' | '\u{2028}' | '\u{2029}' => break,
Expand Down

0 comments on commit fd616c8

Please sign in to comment.