Uninitialized variables: error when passing uninit pointer to function

This commit is contained in:
Daniel Marjamäki 2013-12-10 18:36:16 +01:00
parent 83066edec0
commit ef15e40de5
2 changed files with 11 additions and 3 deletions

View File

@ -1693,9 +1693,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
return true;
if (Token::Match(argStart, "const %type% & %var% [,)]"))
return true;
if (pointer && !address && Token::Match(argStart, "%type% * %var% [,)]"))
if (pointer && !address && Token::Match(argStart, "struct| %type% * %var% [,)]"))
return true;
if ((pointer || address) && Token::Match(argStart, "const %type% * %var% [,)]"))
if ((pointer || address) && Token::Match(argStart, "const struct| %type% * %var% [,)]"))
return true;
if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]"))
return true;
@ -1830,7 +1830,7 @@ bool CheckUninitVar::isMemberVariableUsage(const Token *tok, bool isPointer, boo
if (Token::Match(tok, "%var% . %var%") && tok->strAt(2) == membervar)
return true;
else if (Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc, _tokenizer->isCPP()))
else if (!isPointer && Token::Match(tok->previous(), "[(,] %var% [,)]") && isVariableUsage(tok, isPointer, alloc, _tokenizer->isCPP()))
return true;
return false;

View File

@ -2649,6 +2649,14 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
checkUninitVar2("typedef struct { int a; int b; } AB;\n"
"void a(AB *ab);\n"
"void b() {\n"
" AB *ab;\n"
" a(ab);\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str());
checkUninitVar2("void a(const char *c);\n" // const address => error
"void b() {\n"
" char *c;\n"