From 75b8822f06c21825f723ea42adef5dfb8d5e9830 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 28 May 2020 19:22:11 +0300 Subject: [PATCH] scanner: prevent infinite looping, when reaching .eof due to parser bugs --- vlib/v/scanner/scanner.v | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 276971df86..17fd49b1ac 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -44,6 +44,7 @@ pub mut: is_fmt bool // Used only for skipping ${} in strings, since we need literal // string values when generating formatted code. comments_mode CommentsMode + eofs int } pub enum CommentsMode { @@ -521,8 +522,18 @@ fn (mut s Scanner) skip_whitespace() { } fn (mut s Scanner) end_of_file() token.Token { + s.eofs++ + if s.eofs > 50 { + s.line_nr-- + s.error('the end of file `$s.file_path` has been reached 50 times already, the v parser is probably stuck.\n' + + 'This should not happen. Please report the bug here, and include the last 2-3 lines of your source code:\n' + + 'https://github.com/vlang/v/issues/new?labels=Bug&template=bug_report.md' + ) + } + if s.pos != s.text.len && s.eofs == 1 { + s.inc_line_number() + } s.pos = s.text.len - s.inc_line_number() return s.new_token(.eof, '', 1) }