Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8944,6 +8944,15 @@ void Tokenizer::findGarbageCode() const
"typedef",
"while" };

const auto isTooLargeInt = [](const Token *tok) {
try {
(void)MathLib::toBigUNumber(tok);
return false;
} catch (const InternalError & /*e*/) {
return true;
}
};

for (const Token *tok = tokens(); tok; tok = tok->next()) {
// initialization: = {
if (Token::simpleMatch(tok, "= {") && Token::simpleMatch(tok->linkAt(1), "} ("))
Expand Down Expand Up @@ -8995,6 +9004,19 @@ void Tokenizer::findGarbageCode() const
syntaxError(tok);
}

// Invalid integer literal as argument of a possible macro
if (tok->isNumber() && MathLib::isInt(tok->str()) && isTooLargeInt(tok)) {
const Token *prev = tok->previous();
while (prev && !Token::Match(prev, "(|;|{|}")) {
if (Token::Match(prev, ")|]"))
prev = prev->link();
prev = prev->previous();
}
if (prev && prev->str() == "(" && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword())
unknownMacroError(prev->previous());
syntaxError(tok, "integer literal too large");
}

if (tok->isControlFlowKeyword()) {
if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... }
if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) {
Expand Down
10 changes: 10 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(checkConfiguration);

TEST_CASE(unknownMacroBeforeReturn);
TEST_CASE(unknownMacroTooLargeInt);

TEST_CASE(cppKeywordInCSource);

Expand Down Expand Up @@ -8853,6 +8854,15 @@ class TestTokenizer : public TestFixture {
ASSERT_THROW_INTERNAL(tokenizeAndStringify("int f() { X return 0; }\n"), UNKNOWN_MACRO);
}

void unknownMacroTooLargeInt() {
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR(123456789012345678901234567890123456789);\n"),
UNKNOWN_MACRO,
"There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it.");
ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"),
SYNTAX,
"syntax error: integer literal too large");
}

void cppKeywordInCSource() {
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}\n", dinit(TokenizeOptions, $.cpp = false)));

Expand Down
Loading