Fixed recently introduced FP in checkuninitvar.cpp if address of variable is passed to a function

This commit is contained in:
PKEuS 2015-01-22 21:22:02 +01:00
parent 4c76f71499
commit da5a04f071
2 changed files with 20 additions and 2 deletions

View File

@ -1745,12 +1745,12 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
// is this a function call? // is this a function call?
if (start && Token::Match(start->previous(), "%var% (")) { if (start && Token::Match(start->previous(), "%var% (")) {
const bool address(vartok->previous()->str() == "&");
// check how function handle uninitialized data arguments.. // check how function handle uninitialized data arguments..
const Function *func = start->previous()->function(); const Function *func = start->previous()->function();
if (func) { if (func) {
const Variable *arg = func->getArgumentVar(argumentNumber); const Variable *arg = func->getArgumentVar(argumentNumber);
if (arg) { if (arg) {
const bool address(vartok->previous()->str() == "&");
const Token *argStart = arg->typeStartToken(); const Token *argStart = arg->typeStartToken();
if (!address && Token::Match(argStart, "struct| %type% [,)]")) if (!address && Token::Match(argStart, "struct| %type% [,)]"))
return true; return true;
@ -1776,7 +1776,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
bool isuninitbad = _settings->library.isuninitargbad(start->previous(), argumentNumber + 1); bool isuninitbad = _settings->library.isuninitargbad(start->previous(), argumentNumber + 1);
if (alloc) if (alloc)
return isnullbad && isuninitbad; return isnullbad && isuninitbad;
return isuninitbad; return isuninitbad && (!address || isnullbad);
} }
} }
} }

View File

@ -2884,6 +2884,24 @@ private:
" return abc.a;\n" " return abc.a;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVarB("void write_packet() {\n"
" time_t now0;\n"
" time(&now0);\n"
"}", "test.c");
ASSERT_EQUALS("", errout.str());
checkUninitVarB("void write_packet() {\n"
" time_t* now0;\n"
" time(now0);\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout.str());
checkUninitVarB("void write_packet() {\n"
" char now0;\n"
" strcmp(&now0, sth);\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout.str());
} }
void uninitvar2_value() { void uninitvar2_value() {