Skip to content

Commit

Permalink
syntax: Prevent bumping the parser EOF to stop infinite loops.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Mar 26, 2016
1 parent 8f34053 commit 6abab49
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ pub struct Parser<'a> {
/// the previous token or None (only stashed sometimes).
pub last_token: Option<Box<token::Token>>,
last_token_interpolated: bool,
last_token_eof: bool,
pub buffer: [TokenAndSpan; 4],
pub buffer_start: isize,
pub buffer_end: isize,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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() ||
Expand Down

0 comments on commit 6abab49

Please sign in to comment.