Skip to content

Commit 6abab49

Browse files
committed
syntax: Prevent bumping the parser EOF to stop infinite loops.
1 parent 8f34053 commit 6abab49

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libsyntax/parse/parser.rs

+11
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ pub struct Parser<'a> {
254254
/// the previous token or None (only stashed sometimes).
255255
pub last_token: Option<Box<token::Token>>,
256256
last_token_interpolated: bool,
257+
last_token_eof: bool,
257258
pub buffer: [TokenAndSpan; 4],
258259
pub buffer_start: isize,
259260
pub buffer_end: isize,
@@ -366,6 +367,7 @@ impl<'a> Parser<'a> {
366367
last_span: span,
367368
last_token: None,
368369
last_token_interpolated: false,
370+
last_token_eof: false,
369371
buffer: [
370372
placeholder.clone(),
371373
placeholder.clone(),
@@ -998,6 +1000,15 @@ impl<'a> Parser<'a> {
9981000

9991001
/// Advance the parser by one token
10001002
pub fn bump(&mut self) {
1003+
if self.last_token_eof {
1004+
// Bumping after EOF is a bad sign, usually an infinite loop.
1005+
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
1006+
}
1007+
1008+
if self.token == token::Eof {
1009+
self.last_token_eof = true;
1010+
}
1011+
10011012
self.last_span = self.span;
10021013
// Stash token for error recovery (sometimes; clone is not necessarily cheap).
10031014
self.last_token = if self.token.is_ident() ||

0 commit comments

Comments
 (0)