Aul: Throw correct exception on parameter-less warning pragma

When no parameter followed the "#warning" pragma, because the end
iterator would be located before the begin iterator, basic_string's
constructor would throw std::length_error. Check for this case
beforehand so we can throw the expected C4ParseError instead.
master
Nicolas Hake 2019-02-17 11:34:35 +01:00
parent 918c4139e8
commit c8d22e321b
2 changed files with 11 additions and 6 deletions

View File

@ -778,12 +778,9 @@ void C4AulParse::UnexpectedToken(const char * Expected)
void C4AulParse::Parse_WarningPragma()
{
assert(SEqual2(TokenSPos, C4AUL_Warning));
assert(std::isspace(TokenSPos[sizeof(C4AUL_Warning) - 1]));
// Read parameters in to string buffer. The sizeof() includes the terminating \0, but
// that's okay because we need to skip (at least) one whitespace character anyway.
std::string line(TokenSPos + sizeof(C4AUL_Warning), SPos);
// Read parameters in to string buffer.
std::string line(TokenSPos + sizeof(C4AUL_Warning) - 1, SPos);
auto end = line.end();
auto cursor = std::find_if_not(begin(line), end, IsWhiteSpace);

View File

@ -88,6 +88,14 @@ TEST(AulSyntaxTest, ParseSyntaxErrors)
EXPECT_THROW(ParseStatement("func Main() {}"), C4AulParseError);
}
TEST(AulSyntaxTest, PragmaWarningWithInsufficientData)
{
EXPECT_THROW(ParseScript("#warning"), C4AulParseError);
EXPECT_THROW(ParseScript("#warning\n"), C4AulParseError);
EXPECT_THROW(ParseScript("#warning "), C4AulParseError);
EXPECT_THROW(ParseScript("#warning \n"), C4AulParseError);
}
using namespace ::aul::ast;
TEST(AulSyntaxTest, ParseLiterals)
{