UninitVar: fixed false negatives for function calls

This commit is contained in:
Daniel Marjamäki 2013-11-16 12:02:21 +01:00
parent 8c7e3d14aa
commit cfd6b0c384
2 changed files with 18 additions and 0 deletions

View File

@ -1628,6 +1628,8 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
const Token *argStart = arg->typeStartToken(); const Token *argStart = arg->typeStartToken();
while (argStart->previous() && argStart->previous()->isName()) while (argStart->previous() && argStart->previous()->isName())
argStart = argStart->previous(); argStart = argStart->previous();
if (!address && Token::Match(argStart, "const| struct| %type% [,)]"))
return true;
if (!address && Token::Match(argStart, "const| struct| %type% %var% [,)]")) if (!address && Token::Match(argStart, "const| struct| %type% %var% [,)]"))
return true; return true;
if (Token::Match(argStart, "const %type% & %var% [,)]")) if (Token::Match(argStart, "const %type% & %var% [,)]"))
@ -1636,6 +1638,8 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool cpp
return true; return true;
if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]")) if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]"))
return true; return true;
if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]"))
return true;
} }
} else if (Token::Match(start->previous(), "if|while|for")) { } else if (Token::Match(start->previous(), "if|while|for")) {

View File

@ -2546,6 +2546,13 @@ private:
// Handling of function calls // Handling of function calls
void uninitvar2_func() { void uninitvar2_func() {
// non-pointer variable // non-pointer variable
checkUninitVar2("void a(char);\n" // value => error
"void b() {\n"
" char c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char c);\n" // value => error checkUninitVar2("void a(char c);\n" // value => error
"void b() {\n" "void b() {\n"
" char c;\n" " char c;\n"
@ -2611,6 +2618,13 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(const char c[]);\n" // const address => error
"void b() {\n"
" char *c;\n"
" a(c);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("void a(char **c);\n" // address of pointer => no error checkUninitVar2("void a(char **c);\n" // address of pointer => no error
"void b() {\n" "void b() {\n"
" char *c;\n" " char *c;\n"