Fix #3256 (Null pointer dereference not detected)

http://sourceforge.net/apps/trac/cppcheck/ticket/3256
It is now detected if --inconclusive command line argument is given (the argument is unofficial currently)
This commit is contained in:
Reijo Tomperi 2011-10-29 11:35:31 +03:00
parent 950460c0a7
commit 5e0e2c4782
2 changed files with 16 additions and 3 deletions

View File

@ -817,9 +817,14 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (Token::simpleMatch(tok2, ") ;") &&
(Token::Match(tok2->link()->tokAt(-2), "[;{}.] %var% (") ||
Token::Match(tok2->link()->tokAt(-5), "[;{}] ( * %var% ) ("))) {
// noreturn function?
if (tok2->strAt(2) == "}")
break;
if (!_settings->inconclusive) {
// noreturn function?
// If inside null pointer check we unknown function call, we must
// assume that it can terminate the program and possible null pointer
// error wont ever happen.
if (tok2->strAt(2) == "}")
break;
}
// init function (global variables)
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();

View File

@ -1176,6 +1176,14 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(char *p) {\n"
" if (!p) {\n"
" abort();\n"
" }\n"
" *p = 0;\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str());
check("void foo(char *p) {\n"
" if (!p) {\n"
" (*bail)();\n"