Repository navigation
Fix #15216 Overlapping warnings uninitvar, uninitStructMember - #9035
chrchr-github wants to merge 8 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerinpr/andcppcheckinbase/. 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]anderror: 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.
|
|
||
| void CheckUninitVarImpl::uninitStructMemberError(const Token *tok, const std::string &membername) | ||
| { | ||
| if (diag(tok, /*insert*/ false)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
The severity change is expected, uninitStructMember is always an error regardless of condtions etc.
|
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/ 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
left a comment
There was a problem hiding this comment.
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 | |
| 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
cppcheckandtestrunnerinpr/andcppcheckinbase/. Both builds succeeded. -
./testrunner TestUninitVar: 70 tests, 0 failures. -
pr/cppcheck --errorlist | grep -c 'id="uninitStructMember"'gives1, so the earlier regression is fixed. -
I checked
test/cfg/gtk.cwith--library=gtk --inline-suppr. The new// cppcheck-suppress uninitvarat line 686 matches, and lines 680–689 report nothing, including nounmatchedSuppression. -
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
uninitvarfrom base is still reported in the PR. Only theuninitStructMembermessages on the same expression went away; base had three, at lines 4, 7 and 10. -
Suppressions: with
// cppcheck-suppress uninitvar(or--suppress=uninitvar) onreturn s.x;, base still reportsuninitStructMemberand the PR reports nothing. This looks intended and matches the change ingtk.c. -
With a possible value (
if (c) s.x = 1; h(s.x);), the PR now reports onlywarning: uninitvar, with or without--enable=warning. C1 already mentioned this.
Automated review of 65abef8 by Claude. The pull request was built and tested locally.
No description provided.