Skip to content

Fix #15300: internalError with invalid integer literal in unknown macro - #9106

Open
ludviggunne wants to merge 4 commits into
cppcheck-opensource:mainfrom
ludviggunne:15300-macro-big-number
Open

ludviggunne wants to merge 4 commits into
cppcheck-opensource:mainfrom
ludviggunne:15300-macro-big-number

Conversation

@ludviggunne

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

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 PR turns the internalError for integer literals that are too large into either unknownMacro or syntaxError: integer literal too large. In every case I tried, the PR reports an error where base reported internalError, and none of my tests found a new false positive where base was clean.

The main problem is the unknownMacro heuristic. It fires for any name( that is not a keyword and encloses the literal, including ordinary and library function calls. For example, printf("%llu", 18446744073709551616); makes cppcheck ask the user to configure printf as a macro, which is misleading. Before choosing unknownMacro, the check should at least skip known functions (a declared function or one in the library configuration).

There is also a smaller point: the CLI test test_internal_error_loc_int no longer checks the location of an internal error, which was its purpose.

Comments

# Comment By Where Status
1 Known/declared functions (e.g. printf) are reported as unknownMacro AI review lib/tokenize.cpp:9016 🆕 New
2 test_internal_error_loc_int no longer tests an internalError location AI review test/cli/other_test.py:2950 🆕 New

@ludviggunne, please fix, resolve or reply to the open and new comments: #1, #2.

Testing

I built cppcheck and testrunner in pr/, and cppcheck in base/.

  • ./testrunner TestTokenizer: 0 failures.
  • test/cli/other_test.py: 10 failures (the test_rule* and test_redundant_file_reads tests). The same tests fail on base, so they come from the environment. The changed test_internal_error_loc_int passes.

I compared pr and base on many inputs:

  • an unknown macro with nested parentheses or brackets
  • if, return (...), sizeof, static_assert, an enum, a template, a lambda, a braced initializer
  • __int128 literals
  • digit separators, the uz, ULL, wb and uwb suffixes, user-defined literals
  • asm

The PR reports syntaxError or unknownMacro exactly where base reported internalError. Valid literals produced no new errors.

Problem: misleading unknownMacro for real function calls

#include <stdio.h>
void f() { printf("%llu", 18446744073709551616); }
void g(unsigned __int128 v);
void h() { g(0x100000000000000000000ULL); }

/work/pr/cppcheck -q p.cpp:

p.cpp:2:12: error: There is an unknown macro here somewhere. Configuration is required. If printf is a macro then please configure it. [unknownMacro]

After removing the printf line:

p.cpp:3:12: error: There is an unknown macro here somewhere. Configuration is required. If g is a macro then please configure it. [unknownMacro]

void foo(unsigned long long); void f() { foo(0x10000000000000000); } gives the same result. In all three cases the function is known, so the output should be the plain syntaxError: integer literal too large.


Automated review of d21d266 by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp Resolved
Comment thread test/cli/other_test.py Resolved

@dmcppcheck dmcppcheck left a comment

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

I found one new problem. The new check throws at the first too-large literal in findGarbageCode's token loop, before the other checks have looked at the rest of the code. So code that base reported as unknownMacro is now reported only as syntaxError: integer literal too large. Example: int x = STR(a; 1234…789);. A smaller gap: the walk back from the literal stops at {. Literals inside braces or a lambda in a macro argument therefore get syntaxError instead of unknownMacro. In every other case I tried, the PR turns base's internalError into a proper error and adds no new false positives.

Comments

# Comment By Where Status
1 unknownMacro is suggested for known/declared functions such as printf AI review lib/tokenize.cpp:9016 💬 Answered: Author: an invalid literal that is not in a macro does not compile anyway, so this is not needed
2 test_internal_error_loc_int no longer tests the location of an internalError AI review test/cli/other_test.py:2950 ✅ Fixed
3 New check runs too early and replaces an existing unknownMacro error with syntaxError AI review lib/tokenize.cpp:9008 🆕 New
4 Literal inside braces/lambda in macro argument is not reported as unknownMacro AI review lib/tokenize.cpp:9010 🆕 New (false negative)

@ludviggunne, please fix, resolve or reply to the open and new comments: #3, #4.
For #4 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.

Testing

I built cppcheck and testrunner in pr/, and cppcheck in base/.

  • ./testrunner TestTokenizer TestMathLib: 0 failures.
  • pytest test/cli/other_test.py -k loc_int: 1 passed.

I compared pr and base on the following inputs:

  • user-defined literals, binary literals, digit separators
  • sizeof, if, braced initializers, asm
  • floating-point and hex-float literals, the wb suffix
  • the boundary values 18446744073709551615, 0xFFFFFFFFFFFFFFFF and -9223372036854775808

The boundary values give no error, as expected. Wherever base reported internalError, the PR now reports a proper error.

Regression: unknownMacro is replaced by syntaxError

int x = STR(a; 123456789012345678901234567890123456789);

cppcheck -q p.cpp:

pr:   p.cpp:1:16: error: syntax error: integer literal too large [syntaxError]
base: p.cpp:1:9: error: There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it. [unknownMacro]

The same happens in C with struct S s = MAC(.a = { 1234...789 });. Base reports syntax error: ( . a at column 18. The PR reports the literal error at column 25 instead.

Macro arguments with braces are not detected as unknownMacro

int x = STR({ 123456789012345678901234567890123456789 });
void f() { M([]{ return 123456789012345678901234567890123456789; }); }

Both give syntax error: integer literal too large with the PR (base gave internalError). The heuristic stops at {, so STR and M are not suggested as macros.


Automated review of 2bdc59e by Claude. The pull request was built and tested locally.

Comment thread lib/tokenize.cpp
}

// 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.

Comment thread lib/tokenize.cpp
// 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, "(|;|{|}")) {

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 (.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants