Commit dc0fa46a by Corentin Wallez Committed by Commit Bot

preprocessor: Check for line number overflow

Also remove dead code in Tokenizer.l BUG=chromium:668842 Change-Id: Ice18313a64f0bb2242299993bfaa882a6578ad54 Reviewed-on: https://chromium-review.googlesource.com/435042Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Commit-Queue: Corentin Wallez <cwallez@chromium.org>
parent 80cdc376
...@@ -63,6 +63,7 @@ class Diagnostics ...@@ -63,6 +63,7 @@ class Diagnostics
PP_INVALID_LINE_DIRECTIVE, PP_INVALID_LINE_DIRECTIVE,
PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3, PP_NON_PP_TOKEN_BEFORE_EXTENSION_ESSL3,
PP_UNDEFINED_SHIFT, PP_UNDEFINED_SHIFT,
PP_TOKENIZER_ERROR,
PP_ERROR_END, PP_ERROR_END,
PP_WARNING_BEGIN, PP_WARNING_BEGIN,
......
...@@ -19,6 +19,8 @@ struct Token ...@@ -19,6 +19,8 @@ struct Token
{ {
enum Type enum Type
{ {
// Calling this ERROR causes a conflict with wingdi.h
GOT_ERROR = -1,
LAST = 0, // EOF. LAST = 0, // EOF.
IDENTIFIER = 258, IDENTIFIER = 258,
......
...@@ -110,7 +110,14 @@ FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".") ...@@ -110,7 +110,14 @@ FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".")
"/*" { BEGIN(COMMENT); } "/*" { BEGIN(COMMENT); }
<COMMENT>[^*\r\n]+ <COMMENT>[^*\r\n]+
<COMMENT>"*" <COMMENT>"*"
<COMMENT>{NEWLINE} { ++yylineno; } <COMMENT>{NEWLINE} {
if (yylineno == INT_MAX)
{
*yylval = "Integer overflow on line number";
return pp::Token::GOT_ERROR;
}
++yylineno;
}
<COMMENT>"*/" { <COMMENT>"*/" {
yyextra->leadingSpace = true; yyextra->leadingSpace = true;
BEGIN(INITIAL); BEGIN(INITIAL);
...@@ -237,13 +244,16 @@ FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".") ...@@ -237,13 +244,16 @@ FRACTIONAL_CONSTANT ({DIGIT}*"."{DIGIT}+)|({DIGIT}+".")
[ \t\v\f]+ { yyextra->leadingSpace = true; } [ \t\v\f]+ { yyextra->leadingSpace = true; }
{NEWLINE} { {NEWLINE} {
if (yylineno == INT_MAX)
{
*yylval = "Integer overflow on line number";
return pp::Token::GOT_ERROR;
}
++yylineno; ++yylineno;
yylval->assign(1, '\n'); yylval->assign(1, '\n');
return '\n'; return '\n';
} }
\\{NEWLINE} { ++yylineno; }
. { . {
yylval->assign(1, yytext[0]); yylval->assign(1, yytext[0]);
return pp::Token::PP_OTHER; return pp::Token::PP_OTHER;
...@@ -318,7 +328,18 @@ void Tokenizer::setMaxTokenSize(size_t maxTokenSize) ...@@ -318,7 +328,18 @@ void Tokenizer::setMaxTokenSize(size_t maxTokenSize)
void Tokenizer::lex(Token *token) void Tokenizer::lex(Token *token)
{ {
token->type = yylex(&token->text, &token->location, mHandle); int tokenType = yylex(&token->text, &token->location, mHandle);
if (tokenType == Token::GOT_ERROR)
{
mContext.diagnostics->report(Diagnostics::PP_TOKENIZER_ERROR, token->location, token->text);
token->type = Token::LAST;
}
else
{
token->type = tokenType;
}
if (token->text.size() > mMaxTokenSize) if (token->text.size() > mMaxTokenSize)
{ {
mContext.diagnostics->report(Diagnostics::PP_TOKEN_TOO_LONG, mContext.diagnostics->report(Diagnostics::PP_TOKEN_TOO_LONG,
......
...@@ -267,6 +267,36 @@ TEST_F(LocationTest, LineDirectiveMissingNewline) ...@@ -267,6 +267,36 @@ TEST_F(LocationTest, LineDirectiveMissingNewline)
mPreprocessor.lex(&token); mPreprocessor.lex(&token);
} }
// Test for an error being generated when the line number overflows - regular version
TEST_F(LocationTest, LineOverflowRegular)
{
const char *str = "#line 2147483647\n\n";
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
using testing::_;
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_TOKENIZER_ERROR, _, _));
pp::Token token;
mPreprocessor.lex(&token);
}
// Test for an error being generated when the line number overflows - inside /* */ comment version
TEST_F(LocationTest, LineOverflowInComment)
{
const char *str = "#line 2147483647\n/*\n*/";
ASSERT_TRUE(mPreprocessor.init(1, &str, NULL));
using testing::_;
// Error reported about EOF.
EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_TOKENIZER_ERROR, _, _));
pp::Token token;
mPreprocessor.lex(&token);
}
struct LineTestParam struct LineTestParam
{ {
const char* str; const char* str;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment