Skip to content

Commit 39d612b

Browse files
eddybpnkfelix
authored andcommitted
syntax: Prevent bumping the parser EOF to stop infinite loops.
1 parent ef6e1f1 commit 39d612b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ pub struct Parser<'a> {
261261
/// the previous token or None (only stashed sometimes).
262262
pub last_token: Option<Box<token::Token>>,
263263
last_token_interpolated: bool,
264+
last_token_eof: bool,
264265
pub buffer: [TokenAndSpan; 4],
265266
pub buffer_start: isize,
266267
pub buffer_end: isize,
@@ -369,6 +370,7 @@ impl<'a> Parser<'a> {
369370
last_span: span,
370371
last_token: None,
371372
last_token_interpolated: false,
373+
last_token_eof: false,
372374
buffer: [
373375
placeholder.clone(),
374376
placeholder.clone(),
@@ -982,6 +984,15 @@ impl<'a> Parser<'a> {
982984

983985
/// Advance the parser by one token
984986
pub fn bump(&mut self) {
987+
if self.last_token_eof {
988+
// Bumping after EOF is a bad sign, usually an infinite loop.
989+
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
990+
}
991+
992+
if self.token == token::Eof {
993+
self.last_token_eof = true;
994+
}
995+
985996
self.last_span = self.span;
986997
// Stash token for error recovery (sometimes; clone is not necessarily cheap).
987998
self.last_token = if self.token.is_ident() ||

0 commit comments

Comments
 (0)