Skip to content

Commit

Permalink
fix: avoid stack overflow on many comments in a row (#7325)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Feb 7, 2025
1 parent 09d7705 commit ac1da8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
26 changes: 18 additions & 8 deletions compiler/noirc_frontend/src/lexer/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,28 @@ impl<'a> Lexer<'a> {
}

fn next_token(&mut self) -> SpannedTokenResult {
if !self.skip_comments {
return self.next_token_without_checking_comments();
}

// Read tokens and skip comments. This is done like this to avoid recursion
// and hitting stack overflow when there are many comments in a row.
loop {
let token = self.next_token_without_checking_comments()?;
if matches!(token.token(), Token::LineComment(_, None) | Token::BlockComment(_, None)) {
continue;
}
return Ok(token);
}
}

/// Reads the next token, which might be a comment token (these aren't skipped in this method)
fn next_token_without_checking_comments(&mut self) -> SpannedTokenResult {
match self.next_char() {
Some(x) if Self::is_code_whitespace(x) => {
let spanned = self.eat_whitespace(x);
if self.skip_whitespaces {
self.next_token()
self.next_token_without_checking_comments()
} else {
Ok(spanned)
}
Expand Down Expand Up @@ -755,10 +772,6 @@ impl<'a> Lexer<'a> {
return Err(LexerErrorKind::NonAsciiComment { span });
}

if doc_style.is_none() && self.skip_comments {
return self.next_token();
}

Ok(Token::LineComment(comment, doc_style).into_span(start, self.position))
}

Expand Down Expand Up @@ -804,9 +817,6 @@ impl<'a> Lexer<'a> {
return Err(LexerErrorKind::NonAsciiComment { span });
}

if doc_style.is_none() && self.skip_comments {
return self.next_token();
}
Ok(Token::BlockComment(content, doc_style).into_span(start, self.position))
} else {
let span = Span::inclusive(start, self.position);
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4364,3 +4364,9 @@ fn errors_on_if_without_else_type_mismatch() {
};
assert!(matches!(**err, TypeCheckError::TypeMismatch { .. }));
}

#[test]
fn does_not_stack_overflow_on_many_comments_in_a_row() {
let src = "//\n".repeat(10_000);
assert_no_errors(&src);
}

0 comments on commit ac1da8f

Please sign in to comment.