Fixed #5108 (false positive: uninitialized variable (assignment in function parameter))

This commit is contained in:
Daniel Marjamäki 2013-11-04 23:09:15 +01:00
parent 6ad2491817
commit 7996d609be
3 changed files with 9 additions and 3 deletions

View File

@ -245,7 +245,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
} }
// 2nd parameter.. // 2nd parameter..
if ((value == 0 && Token::Match(secondParam, "0|NULL ,|)")) || (secondParam && secondParam->varId() > 0)) { if ((value == 0 && Token::Match(secondParam, "0|NULL ,|)")) || (secondParam && secondParam->varId() > 0 && Token::Match(secondParam->next(),"[,)]"))) {
if (functionNames2_all.find(tok.str()) != functionNames2_all.end()) if (functionNames2_all.find(tok.str()) != functionNames2_all.end())
var.push_back(secondParam); var.push_back(secondParam);
else if (value == 0 && functionNames2_nullptr.find(tok.str()) != functionNames2_nullptr.end()) else if (value == 0 && functionNames2_nullptr.find(tok.str()) != functionNames2_nullptr.end())
@ -302,7 +302,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
continue; continue;
if ((*i == 'n' || *i == 's' || scan) && (!scan || value == 0)) { if ((*i == 'n' || *i == 's' || scan) && (!scan || value == 0)) {
if ((value == 0 && argListTok->str() == "0") || (argListTok->varId() > 0)) { if ((value == 0 && argListTok->str() == "0") || (argListTok->varId() > 0 && Token::Match(argListTok,"%var% [,)]"))) {
var.push_back(argListTok); var.push_back(argListTok);
} }
} }

View File

@ -1264,7 +1264,7 @@ private:
" char cBuf[10];\n" " char cBuf[10];\n"
" sprintf(cBuf, \"%s\", c ? c : \"0\" );\n" " sprintf(cBuf, \"%s\", c ? c : \"0\" );\n"
"}"); "}");
TODO_ASSERT_EQUALS("","[test.cpp:4]: (error) Possible null pointer dereference: c\n", errout.str()); ASSERT_EQUALS("",errout.str());
} }
void nullpointer24() { // #5083 - fp: chained assignment void nullpointer24() { // #5083 - fp: chained assignment

View File

@ -1240,6 +1240,12 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f() {\n" // Ticket #5108 (fp)
" const char *a;\n"
" printf(\"%s\", a=\"abc\");\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // Ticket #3497 checkUninitVar("void f() {\n" // Ticket #3497
" char header[1];\n" " char header[1];\n"
" *((unsigned char*)(header)) = 0xff;\n" " *((unsigned char*)(header)) = 0xff;\n"