Skip to content

Fix #15216 Overlapping warnings uninitvar, uninitStructMember - #9035

Open
chrchr-github wants to merge 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15216
Open

chrchr-github wants to merge 8 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15216

Conversation

@chrchr-github

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 removes uninitStructMember from --errorlist. diag() returns true when tok is null. The new early return in uninitStructMemberError() therefore also fires for the nullptr call in getErrorMessages() (lib/checkuninitvar.cpp:1833), so the message is never reported there. I reproduced this: base lists the ID once, the PR lists it zero times. Tools and documentation that rely on the error list, and the GUI's list of checkers, would lose the ID. The fix is to guard the call with tok &&, the way uninitvarError() does.

Apart from that, the dedup works in every struct-access pattern I tried (s.x, g(s), (&s)->y, t = s, return s, h(s.y)). It also removes the per-member duplicates when several members are uninitialized. One side effect: when ValueFlow reports only a possible uninit value, the error: uninitStructMember is now dropped in favour of a warning: uninitvar for the same token. In effect the diagnostic is downgraded from error to warning. That seems acceptable, but it isn't covered by a test.

Testing

  • Built cppcheck and testrunner in pr/ and cppcheck in base/. Both builds succeeded.
  • ./testrunner TestUninitVar: 70 tests, 0 failures.

Regression: the error list loses uninitStructMember

$ base/cppcheck --errorlist | grep -c 'id="uninitStructMember"'
1
$ pr/cppcheck --errorlist | grep -c 'id="uninitStructMember"'
0

Cause: getErrorMessages() calls c.uninitStructMemberError(nullptr, "a.b"), and diag(nullptr, false) returns true.

Dedup checks (scratch/a.c, scratch/b.c)
I ran various struct accesses: return s.x, g(s) with one or two uninitialized members, return s, p->x via a pointer, h(s.y), (&s)->y, t = s, and nested g(t.in). In base, each uninitStructMember came together with an uninitvar on the same expression. In the PR only uninitvar remains, and no uninitvar diagnostics were lost.

Severity change (scratch/c.c)

struct S { int x; int y; };
void h(int);
void f(int c) {
    struct S s;
    if (c) s.x = 1;
    h(s.x);
}
  • Base reports both warning: Uninitialized variable: s.x [uninitvar] and error: Uninitialized struct member: s.x [uninitStructMember].
  • The PR reports only the warning. That is probably intended, but the case is not tested.

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

Comment thread lib/checkuninitvar.cpp Outdated

void CheckUninitVarImpl::uninitStructMemberError(const Token *tok, const std::string &membername)
{
if (diag(tok, /*insert*/ false))

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

diag() returns true for a null tok, so this also returns early for getErrorMessages(), which calls c.uninitStructMemberError(nullptr, "a.b"). As a result uninitStructMember no longer appears in cppcheck --errorlist: base prints it once, this PR prints it zero times. Suggest if (tok && diag(tok, /*insert*/ false)), the same guard uninitvarError() uses.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The severity change is expected, uninitStructMember is always an error regardless of condtions etc.

@dmcppcheck

Copy link
Copy Markdown
Contributor

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

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9035/
packages: 100
Differing warnings: 58 (+0 -58)
AI review: 50 improvement (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9035/2026-11-16_11-38-45-65abef8608a2-review.txt)
Timing: +0.9% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9035/2026-11-16_11-38-45-65abef8608a2_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 --errorlist regression from the earlier review (C1/C2) is fixed: the call now has a tok && guard, and uninitStructMember is listed once again. Wherever uninitvar fires on the same expression, uninitStructMember is now gone, and in my tests no uninitvar diagnostic was lost. One side effect is new: a suppression of uninitvar now also hides the uninitStructMember for the same expression, which the gtk.c change seems to expect. The downgrade from error to warning for possible values was already raised in C1.

Earlier comments

# Comment By Where Status
1 uninitStructMember disappears from --errorlist because diag(nullptr) returns true AI review general ✅ Fixed
2 The downgrade from error to warning for possible values is not covered by a test AI review general ⚠️ Open
3 Guard the diag() call with tok && so that the error list keeps uninitStructMember AI review lib/checkuninitvar.cpp:1625 ✅ Fixed

@chrchr-github, please fix, resolve or reply to the open comments: #2.

Testing

  • Built cppcheck and testrunner in pr/ and cppcheck in base/. Both builds succeeded.

  • ./testrunner TestUninitVar: 70 tests, 0 failures.

  • pr/cppcheck --errorlist | grep -c 'id="uninitStructMember"' gives 1, so the earlier regression is fixed.

  • I checked test/cfg/gtk.c with --library=gtk --inline-suppr. The new // cppcheck-suppress uninitvar at line 686 matches, and lines 680–689 report nothing, including no unmatchedSuppression.

  • I compared base and PR on scratch C/C++ cases with --enable=warning:

    • a call to a member function reading an uninitialized member;
    • passing a partly initialized struct by value;
    • an array of structs;
    • access through a pointer to a local struct;
    • copying a struct, then reading the copy;
    • a nested struct member;
    • a member that is only possibly uninitialized.

    Every uninitvar from base is still reported in the PR. Only the uninitStructMember messages on the same expression went away; base had three, at lines 4, 7 and 10.

  • Suppressions: with // cppcheck-suppress uninitvar (or --suppress=uninitvar) on return s.x;, base still reports uninitStructMember and the PR reports nothing. This looks intended and matches the change in gtk.c.

  • With a possible value (if (c) s.x = 1; h(s.x);), the PR now reports only warning: uninitvar, with or without --enable=warning. C1 already mentioned this.


Automated review of 65abef8 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.

2 participants