Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop in lexer when reading a string #20

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading