I found if the dealloc use is in a while loop, then cppcheck won't warn this. Here is a sample file test.cpp
test.cpp
int do_something() { while(1){ int* p=new int[1]; delete[] p; cout<<p[0]; return 0; } } void foo() { int *nc = new int[10]; delete[] nc; cout<<nc[0]; } int main(int argc, char* argv[]) { return 0; }
And cppcheck(commit e2fa3cb10932d6eaa1a228cbfa6822e18500a922) has no warning of cout<<p[0]
cout<<p[0]
Checking test.cpp ... test.cpp:14:11: error: Dereferencing 'nc' after it is deallocated / released [deallocuse] cout<<nc[0]; ^ test.cpp:5:12: error: Memory is allocated but not initialized: p [uninitdata] delete[] p; ^ test.cpp:13:14: error: Memory is allocated but not initialized: nc [uninitdata] delete[] nc;
@danielmarjamaki Could you take a look at this question :)
Probably covered by https://trac.cppcheck.net/ticket/11786
Can we just add a check in lib/checkleakautovar.cpp:646 to handle loops like this?
lib/checkleakautovar.cpp:646
else if (Token::Match(tok, "for|while|do")) { continue; } // unknown control.. (TODO: handle loops) else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) { varInfo.clear(); return false; }
PRs go here: https://github.com/danmar/cppcheck/
Log in to post a comment.
I found if the dealloc use is in a while loop, then cppcheck won't warn this.
Here is a sample file
test.cpp
And cppcheck(commit e2fa3cb10932d6eaa1a228cbfa6822e18500a922) has no warning of
cout<<p[0]
@danielmarjamaki Could you take a look at this question :)
Probably covered by https://trac.cppcheck.net/ticket/11786
Can we just add a check in
lib/checkleakautovar.cpp:646
to handle loops like this?PRs go here: https://github.com/danmar/cppcheck/