Repository navigation
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the reported case (#11023) and for variants such as delete c, delete[] (c) and delete[] (c+1). True positives where the data is read (delete c[0], delete[] *c, delete s->p, an uninitialized pointer) are still reported. One new false negative: the check skips every delete of a non-dereferenced pointer, so it no longer warns when the destructor of the deleted type reads members that were never initialized (struct V { int x; ~V(){ if (x) {} } }; V* v = new V; delete v;). Base reported that case. I found no false positives or crashes.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | delete of a class whose destructor reads uninitialized members is no longer reported | AI review | lib/checkuninitvar.cpp:1257 |
🆕 New (false negative) |
@danmar, please fix, resolve or reply to the open and new comments: #1.
For #1 (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
Built cppcheck and testrunner in pr/, and cppcheck in base/; both builds succeeded. ./testrunner TestUninitVar: 70 tests, 0 failed.
I compared base and PR on scratch files with cppcheck -q --enable=all --inconclusive:
- False positive gone:
new char[12]; delete[] c;,new char; delete c;,delete[] (c);anddelete[] (c+1);. - Still reported, as in base:
delete c[0],delete[] *c,delete s->p, a read ofc[0]or*cbefore the delete, and an uninitialized pointer passed to delete.
New false negative:
struct U { int x; ~U(); };
void g1() { U* u = new U; delete u; }
struct V { int x; ~V() { if (x) {} } };
void g2() { V* v = new V; delete v; }
void g3() { V* v = new V[2]; delete [] v; }cppcheck -q --enable=warning b.cpp
- base:
uninitdataon lines 2, 4 and 5 - PR: no output
Not run: the full test suite and the Python tests in test/cli (the PR doesn't touch them).
Automated review of 775bfe3 by Claude. The pull request was built and tested locally.
| if (Token::simpleMatch(valueExpr->astParent(), "return")) | ||
| return nullptr; | ||
| // deallocating the memory does not read the data | ||
| if (!derefValue && Token::simpleMatch(valueExpr->astParent(), "delete")) |
There was a problem hiding this comment.
AI review
This returns nullptr for every delete p where p is not dereferenced, whatever the pointed-to type is. For a class with a user-written destructor that reads members, the delete does read the uninitialized data:
struct V { int x; ~V() { if (x) {} } };
void g2() { V* v = new V; delete v; }
void g3() { V* v = new V[2]; delete [] v; }Base reports uninitdata for both functions; the PR reports nothing. One option is to skip only when the pointed-to type is trivially destructible, i.e. it is not a class with a user-declared destructor. A test for this case would also help.
|
Test results for commit 775bfe3 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9089/ 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
The new isDataReadInDestructor() brings back the warning for the case in C2 (a destructor that reads a member directly). It looks only at the destructor body of the exact class and only at that class's own members, so the PR still misses several cases that base reported:
- a destructor of a base class reads its members
- the destructor calls a member function that reads a member
- the destructor of a member object reads data
The scan also ignores the order of statements. When the destructor writes a member before it reads it (~S() { x = 0; if (x) {} }), the PR still reports uninitdata, which is a false positive. Base reported that case too, so it is not a regression. The case from #11023 and the PR's own tests work. I found no crashes or recursion.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | delete of a class whose destructor reads uninitialized members is no longer reported | AI review | lib/checkuninitvar.cpp:1257 |
✅ Fixed |
| 2 | Misses reads in base-class destructors, in member functions called from the destructor, and in member destructors | AI review | lib/checkuninitvar.cpp:1167 |
🆕 New (false negative) |
| 3 | The destructor scan ignores order, so a write followed by a read still warns | AI review | lib/checkuninitvar.cpp:1170 |
🆕 New |
@danmar, please fix, resolve or reply to the open and new comments: #2, #3.
For #2 (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/; both builds succeeded. ./testrunner TestUninitVar: 70 tests, 0 failed.
Cases that base reports and the PR misses (cppcheck -q --enable=warning --inconclusive a.cpp):
struct B { int x; ~B() { if (x) {} } };
struct D : B { int y; };
void f1() { D* d = new D; delete d; } // base dtor reads x
struct E : B { int y; ~E() {} };
void f2() { E* e = new E; delete e; } // base dtor reads x
struct T { int x; void g() { if (x) {} } ~T() { g(); } };
void f4() { T* t = new T; delete t; } // dtor calls member fn that reads x
struct V { int x; ~V() { if (x) {} } };
struct Z { V v; ~Z() { } };
void f10() { Z* p = new Z; delete p; } // member's dtor reads v.x- base:
uninitdataon lines 3, 5, 7 and 10 - PR: no output for any of them
These are still reported: an out-of-class destructor, this->x, an array member a[0] and a virtual destructor. A new V*[2]; delete [] v; (an array of pointers) is no longer reported, which is correct.
False positive that remains:
struct S { int x; ~S() { x = 0; if (x) {} } };
void f1() { S* p = new S; delete p; }Both base and PR report b.cpp:2:34: error: Memory is allocated but not initialized: p [uninitdata]. The PR fixes other cases where the destructor does not read: a constructor that sets x in its body, a local variable that shadows x, and sizeof(x).
I did not run the full test suite or the Python tests in test/cli; the PR does not touch them.
Automated review of b5beb95 by Claude. The pull request was built and tested locally.
| const Scope* body = destructor->functionScope; | ||
| for (const Token* tok = body->bodyStart; tok != body->bodyEnd; tok = tok->next()) { | ||
| const Variable* var = tok->variable(); | ||
| if (!var || var->scope() != vt->typeScope || var->isStatic() || var->hasDefault()) |
There was a problem hiding this comment.
AI review
Only the destructor body of the exact class and only its own members (var->scope() != vt->typeScope) are checked. Base reports all of these and the PR reports none of them:
struct B { int x; ~B() { if (x) {} } };
struct D : B { int y; };
void f1() { D* d = new D; delete d; }
struct E : B { int y; ~E() {} };
void f2() { E* e = new E; delete e; }
struct T { int x; void g() { if (x) {} } ~T() { g(); } };
void f4() { T* t = new T; delete t; }
struct V { int x; ~V() { if (x) {} } };
struct Z { V v; ~Z() { } };
void f10() { Z* p = new Z; delete p; }To cover these, the function could recurse into the destructors of base classes and of class-type members, and return true conservatively when the destructor calls a member function.
| if (!var || var->scope() != vt->typeScope || var->isStatic() || var->hasDefault()) | ||
| continue; | ||
| if (!var->isPointer() && !var->typeStartToken()->isStandardType() && !var->typeStartToken()->isEnumType()) | ||
| continue; |
There was a problem hiding this comment.
AI review
The loop returns true on the first token that looks like a read. It does not notice that the member was written earlier in the destructor:
struct S { int x; ~S() { x = 0; if (x) {} } };
void f1() { S* p = new S; delete p; }The PR reports Memory is allocated but not initialized: p [uninitdata] here. Base reported it too, so it is not a regression. One way to avoid the warning is to stop scanning a member once it has been assigned.
No description provided.