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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This throws at the first too-large literal it meets in the main loop. The checks later in findGarbageCode that would give a more specific error never run on the rest of the code.

For int x = STR(a; 123456789012345678901234567890123456789);:

  • base: If STR is a macro then please configure it. [unknownMacro]
  • this PR: syntax error: integer literal too large

One fix is to remember the first too-large literal and throw only after the loop has finished. Another is to do this check in a separate pass at the end of findGarbageCode.

const Token *prev = tok->previous();
while (prev && !Token::Match(prev, "(|;|{|}")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The walk back from the literal stops at {, so a literal inside braces or a lambda in a macro argument is not linked to the macro. STR({ 1234...789 }) and M([]{ return 1234...789; }) report syntaxError instead of unknownMacro for STR/M. A { can be skipped with its link, the same way as ) and ], when it is inside an enclosing (.

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());
Comment thread
ludviggunne marked this conversation as resolved.
Resolved
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
4 changes: 2 additions & 2 deletions test/cli/other_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2928,7 +2928,7 @@ def test_outputfile(tmp_path): # #14051
assert out_text.splitlines() == out_exp


def test_internal_error_loc_int(tmp_path):
def test_syntax_error_loc_int(tmp_path):
test_file = tmp_path / 'test.c'
with open(test_file, 'wt') as f:
f.write(
Expand All @@ -2947,7 +2947,7 @@ def test_internal_error_loc_int(tmp_path):
assert exitcode == 0, stdout
assert stdout.splitlines() == []
assert stderr.splitlines() == [
'{}:3:13: error: Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000 [internalError]'.format(test_file)
'{}:3:13: error: syntax error: integer literal too large [syntaxError]'.format(test_file)
Comment thread
ludviggunne marked this conversation as resolved.
Resolved
]


Expand Down
10 changes: 10 additions & 0 deletions test/testmathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ class TestMathLib : public TestFixture {
tok->str("invalid");
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber(tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid");
ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber("invalid", tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid");

// the error location is the token (#13488)
tok->str("0x10000000000000000");
try {
(void)MathLib::toBigUNumber(tok);
assertThrowFail(__FILE__, __LINE__);
} catch (const InternalError &e) {
ASSERT_EQUALS("Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000", e.errorMessage);
ASSERT(e.token == tok);
}
TokenList::deleteTokens(tok);
}

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