Skip to content

Commit

Permalink
- Fixed infinite loop in lexer when reading an unterminated string
Browse files Browse the repository at this point in the history
- Added a test to ensure unterminated strings are caught as an error by the lexer
  • Loading branch information
xenon authored and nadavrot committed Feb 14, 2024
1 parent b854ced commit 08f2327
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions layout/src/gv/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Lexer {
result
}

pub fn read_string(&mut self) -> String {
pub fn read_string(&mut self) -> Token {
let mut result = String::new();
self.read_char();
while self.ch != '"' {
Expand All @@ -173,11 +173,14 @@ impl Lexer {
'l' => '\n',
_ => self.ch,
}
} else if self.ch == '\0' {
// Reached EOF without completing the string
return Token::Error(self.pos);
}
result.push(self.ch);
self.read_char();
}
result
Token::Identifier(result)
}

pub fn next_token(&mut self) -> Token {
Expand Down Expand Up @@ -209,8 +212,7 @@ impl Lexer {
tok = Token::Comma;
}
'"' => {
let value = self.read_string();
tok = Token::Identifier(value);
tok = self.read_string();
}
'-' => {
self.read_char();
Expand Down
12 changes: 12 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ mod tests {
assert!(matches!(lexer.next_token(), Token::EOF));
}

#[test]
fn catch_unterminated_str() {
let mut lexer = Lexer::from_string("digraph { a -> b; \" } ");
assert!(matches!(lexer.next_token(), Token::DigraphKW));
assert!(matches!(lexer.next_token(), Token::OpenBrace));
assert!(matches!(lexer.next_token(), Token::Identifier(_)));
assert!(matches!(lexer.next_token(), Token::ArrowRight));
assert!(matches!(lexer.next_token(), Token::Identifier(_)));
assert!(matches!(lexer.next_token(), Token::Semicolon));
assert!(matches!(lexer.next_token(), Token::Error(_)));
}

#[test]
fn lex_program() {
let program = get_sample_program2();
Expand Down

0 comments on commit 08f2327

Please sign in to comment.