From 63ae9f1415b963bfd6a14bff9df747679c3c5ffe Mon Sep 17 00:00:00 2001 From: Christian Parpart Date: Wed, 15 May 2019 17:25:35 +0200 Subject: [PATCH] [isoltest] TestFileParser: Fixes access of iterator at and beyond iterator ends. --- test/libsolidity/util/TestFileParser.cpp | 12 ++++++++++- test/libsolidity/util/TestFileParser.h | 27 ++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/test/libsolidity/util/TestFileParser.cpp b/test/libsolidity/util/TestFileParser.cpp index cdda6c217..4f61f6911 100644 --- a/test/libsolidity/util/TestFileParser.cpp +++ b/test/libsolidity/util/TestFileParser.cpp @@ -72,6 +72,16 @@ namespace } } +char TestFileParser::Scanner::peek() const noexcept +{ + if (std::distance(m_char, m_line.end()) < 2) + return '\0'; + + auto next = m_char; + std::advance(next, 1); + return *next; +} + vector TestFileParser::parseFunctionCalls() { vector calls; @@ -561,7 +571,7 @@ void TestFileParser::Scanner::scanNextToken() else if (isWhiteSpace(current())) token = selectToken(Token::Whitespace); else if (isEndOfLine()) - token = selectToken(Token::EOS); + token = make_pair(Token::EOS, "EOS"); else throw Error( Error::Type::ParserError, diff --git a/test/libsolidity/util/TestFileParser.h b/test/libsolidity/util/TestFileParser.h index 7ba757ece..ea948a9b7 100644 --- a/test/libsolidity/util/TestFileParser.h +++ b/test/libsolidity/util/TestFileParser.h @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -310,16 +311,30 @@ private: using TokenDesc = std::pair; /// Advances current position in the input stream. - void advance() { ++m_char; } - /// Returns the current character. - char current() const { return *m_char; } - /// Peeks the next character. - char peek() const { auto it = m_char; return *(it + 1); } + void advance() + { + solAssert(m_char != m_line.end(), "Cannot advance beyond end."); + ++m_char; + } + + /// Returns the current character or '\0' if at end of input. + char current() const noexcept + { + if (m_char == m_line.end()) + return '\0'; + + return *m_char; + } + + /// Retrieves the next character ('\0' if that would be at (or beyond) the end of input) + /// without advancing the input stream iterator. + char peek() const noexcept; + /// Returns true if the end of a line is reached, false otherwise. bool isEndOfLine() const { return m_char == m_line.end(); } std::string m_line; - std::string::iterator m_char; + std::string::const_iterator m_char; std::string m_currentLiteral;