From 6abab49029dacfaa616b726f49817213adc1065b Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Sat, 26 Mar 2016 00:13:54 +0200 Subject: [PATCH] syntax: Prevent bumping the parser EOF to stop infinite loops. --- src/libsyntax/parse/parser.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 82715f263c9..c6a237d3827 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -254,6 +254,7 @@ pub struct Parser<'a> { /// the previous token or None (only stashed sometimes). pub last_token: Option>, last_token_interpolated: bool, + last_token_eof: bool, pub buffer: [TokenAndSpan; 4], pub buffer_start: isize, pub buffer_end: isize, @@ -366,6 +367,7 @@ impl<'a> Parser<'a> { last_span: span, last_token: None, last_token_interpolated: false, + last_token_eof: false, buffer: [ placeholder.clone(), placeholder.clone(), @@ -998,6 +1000,15 @@ impl<'a> Parser<'a> { /// Advance the parser by one token pub fn bump(&mut self) { + if self.last_token_eof { + // Bumping after EOF is a bad sign, usually an infinite loop. + self.bug("attempted to bump the parser past EOF (may be stuck in a loop)"); + } + + if self.token == token::Eof { + self.last_token_eof = true; + } + self.last_span = self.span; // Stash token for error recovery (sometimes; clone is not necessarily cheap). self.last_token = if self.token.is_ident() ||