Skip to content

Commit

Permalink
Fix infinite loops in BotTokenParser when looping through characters …
Browse files Browse the repository at this point in the history
…and EOF is reached
  • Loading branch information
ethanmoffat committed Feb 4, 2022
1 parent 8c696e4 commit 48aad2d
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions EOBot/Interpreter/BotTokenParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public BotToken GetNextToken()
do
{
inputChar = Read();
} while (inputChar != '\n');
} while (inputChar != '\n' && !_inputStream.EndOfStream);

LineNumber++;
Column = 1;
Expand All @@ -86,7 +86,7 @@ public BotToken GetNextToken()
if (char.IsLetter(inputChar))
{
var identifier = inputChar.ToString();
while (char.IsLetterOrDigit(Peek()) || Peek() == '_')
while ((char.IsLetterOrDigit(Peek()) || Peek() == '_') && !_inputStream.EndOfStream)
identifier += Read();

var type = Keywords.Contains(identifier)
Expand All @@ -98,7 +98,7 @@ public BotToken GetNextToken()
else if (char.IsDigit(inputChar))
{
var number = inputChar.ToString();
while (char.IsDigit((char)_inputStream.Peek()))
while (char.IsDigit((char)_inputStream.Peek()) && !_inputStream.EndOfStream)
number += Read();
return Token(BotTokenType.Literal, number);
}
Expand All @@ -117,8 +117,12 @@ public BotToken GetNextToken()
case '"':
{
var stringLiteral = string.Empty;
while ((char)_inputStream.Peek() != '"')
while ((char)_inputStream.Peek() != '"' && !_inputStream.EndOfStream)
stringLiteral += Read();

if (_inputStream.EndOfStream)
return Token(BotTokenType.Error, string.Empty);

Read();
return Token(BotTokenType.Literal, stringLiteral);
}
Expand Down

0 comments on commit 48aad2d

Please sign in to comment.