Skip to content

Fix #15159 Wrong varid: function declaration with pointer-to-array parameter of unknown type - #8981

Open
wjakobsson wants to merge 4 commits into
cppcheck-opensource:mainfrom
wjakobsson:fixvarid
Open

wjakobsson wants to merge 4 commits into
cppcheck-opensource:mainfrom
wjakobsson:fixvarid

Conversation

@wjakobsson

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 56694a0 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/
packages: 1000
Differing warnings: 0 (+0 -0)
Timing: -0.2% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/2026-11-02_07-55-37-56694a01575c_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@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 introduces a varid regression. The condition now treats any name(...)[ in the first argument as a declaration. Because of that, global variables initialised with a subscripted function call, such as int x(g(1)[0]);, no longer get a varid. Base handled them correctly. The check should only accept the declarator shape (T (*p)[N] or T (&r)[N]), not every (...)[. The new test case passes and the existing varid tests still pass.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestVarID reports 0 failed tests out of 227.

Regression: scratch/a.cpp:

int* g(int);
int x(g(1)[0]);
int y(g(1)[0] + 1);
A h(T (*p)[16]);
A k(T (*p)[16], int n);
void f() { x = 1; y = 2; }

Command: cppcheck --debug --verbose a.cpp

base:

2: int x@var1 ( g ( 1 ) [ 0 ] ) ;
3: int y@var2 ( g ( 1 ) [ 0 ] + 1 ) ;
4: A h@var3 ( T ( * p@var4 ) [ 16 ] ) ;
6: void f ( ) { x@var1 = 1 ; y@var2 = 2 ; }

pr:

2: int x ( g ( 1 ) [ 0 ] ) ;
3: int y ( g ( 1 ) [ 0 ] + 1 ) ;
4: A h ( T ( * p@var1 ) [ 16 ] ) ;
6: void f ( ) { x@exprUNIQUE = 1 ; y@exprUNIQUE = 2 ; }

The PR fixes h and k. However, the global variables x and y lose their varids, and their later uses become unrelated expressions. I did not find a warning that changes because of this; I only compared the varid dumps.


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

Comment thread lib/tokenize.cpp Resolved
@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 09b35e0 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/
packages: 1000
Differing warnings: 1 (+0 -1)
AI review: 1 improvement (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/2026-11-04_08-14-04-09b35e0908f1-review.txt)
Timing: -0.1% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/2026-11-04_08-14-04-09b35e0908f1_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@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 C3 regression (int x(g(1)[0]); losing its varid) is fixed and has a test now. The new pattern only covers a single *, & or && directly before the name. Other pointer-to-array declarators are still read as variable initialisations, so the function still gets a wrong varid: T (*const p)[16] and T (**p)[16] are examples. I found no crash and no new false positive. One known ambiguity is left: at global scope, int x(g(*q)[0]); with an undeclared q is now treated as a declaration, so x loses its varid. That case is ambiguous anyway and was mentioned in C3.

Comments

# Comment By Where Status
1 int x(g(1)[0]); at global scope loses its varid AI review general ✅ Fixed
2 The (|[ check is too broad; only the declarator form should be excluded, and a test should be added AI review lib/tokenize.cpp:5065 ✅ Fixed
3 Only a single */& declarator is recognised; (*const p)[N] and (**p)[N] still get a wrong varid AI review lib/tokenize.cpp:5062 🆕 New (false negative)

@wjakobsson, please fix, resolve or reply to the open and new comments: #3.
For #3 (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 TestVarID ran 229 tests with 0 failures.

scratch/a.cpp:

A h1(T (&r)[16]);
A h2(T (&&r)[16]);
A h3(T (*const p)[16]);
A h4(T (**p)[16]);
A h5(T (*p)[16][4]);
A h6(T (*p)[16], int n);
int *gp;
int x1(g(*gp)[0]);
int x2(g(*q)[0]);
void f(){ x1=1; x2=2; }

Command: cppcheck --debug --verbose a.cpp. Output of the PR:

1: A h1 ( T ( & r@var1 ) [ 16 ] ) ;
2: A h2 ( T ( && r ) [ 16 ] ) ;
3: A h3@var2 ( T ( * const p@var3 ) [ 16 ] ) ;
4: A h4@var4 ( T ( * * p@var5 ) [ 16 ] ) ;
5: A h5 ( T ( * p@var6 ) [ 16 ] [ 4 ] ) ;
6: A h6 ( T ( * p@var7 ) [ 16 ] , int n@var8 ) ;
...
8: int x1@var10 ( g ( * gp@var11 ) [ 0 ] ) ;
9: int x2 ( g ( * q@var12 ) [ 0 ] ) ;
10: void f ( ) { x1@var10 = 1 ; x2@exprUNIQUE = 2 ; }

Base gives every line a varid, h1 to h6 included. The PR fixes h1, h2, h5 and h6. h3 and h4 still give the function a varid (a false negative of the fix). x2 loses its varid, but that input is ambiguous. In x1, gp gets a fresh varid instead of reusing the existing one; base does the same, so this is not new.

I also checked a declaration inside a struct, inside a namespace and inside a function body (scratch/b.cpp). The struct and namespace cases are fixed. Inside the function body the output only shifts the varid numbers compared to base, and y keeps its varid.


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

Comment thread lib/tokenize.cpp Resolved
@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 84aa8d9 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/
packages: 1000
Differing warnings: 1 (+1 -0)
AI review: 1 improvement (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/2026-11-17_12-19-27-84aa8d9cb789-review.txt)
Timing: +0.0% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8981/2026-11-17_12-19-27-84aa8d9cb789_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@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 no new problems. The earlier points are fixed in the current version. int x(g(1)[0]); keeps its varid and has a test. (*const p)[N], (**p)[N] and (*volatile p)[N] are now recognised as declarators, so the function name no longer gets a varid. The only behaviour change I could find is the ambiguous int x(g(*q)[0]); at global scope with an undeclared q, and the earlier review already mentioned it.

Comments

# Comment By Where Status
1 int x(g(1)[0]); at global scope loses its varid AI review general ✅ Fixed
2 (|[ check too broad; only the declarator form should be excluded, and a test should be added AI review lib/tokenize.cpp:5065 ✅ Fixed
3 (*const p)[N] and (**p)[N] still give the function a wrong varid AI review general ✅ Fixed
4 Only a single */& before the name is recognised; (*const p)[N] and (**p)[N] are not AI review lib/tokenize.cpp:5062 ✅ Fixed

Testing

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

Tests: ./testrunner TestVarID ran 229 tests with 0 failures.

Varid dumps: I compared cppcheck --debug --verbose between base and the PR on scratch/a.cpp, which covers:

  • local initialisations inside a member function: int x(get(*this)[0]);, int y(get(*mp)[0]); with a member pointer, and int z(get(&m)[1]);
  • local initialisations inside a free function: int a(g(*p)[0]); and int b(g(*p++)[0]); with a parameter p
  • function declarations A h3(T (*volatile p)[16]); and A h5(T (& r)[16][2]);

All the local variables keep the same varids as in base. h3 and h5 lose their wrong varid with the PR (base gives them h3@var9 and h5@var11), and p and r still get varids. I found no regressions.


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

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.

3 participants