Uninitialized variables: Fixed false positives when passing allocated pointer to function

This commit is contained in:
Daniel Marjamäki 2013-12-11 05:54:42 +01:00
parent ef15e40de5
commit a4f9cb78d5
2 changed files with 10 additions and 2 deletions

View File

@ -1693,9 +1693,9 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all
return true; return true;
if (Token::Match(argStart, "const %type% & %var% [,)]")) if (Token::Match(argStart, "const %type% & %var% [,)]"))
return true; return true;
if (pointer && !address && Token::Match(argStart, "struct| %type% * %var% [,)]")) if (pointer && !address && !alloc && Token::Match(argStart, "struct| %type% * %var% [,)]"))
return true; return true;
if ((pointer || address) && Token::Match(argStart, "const struct| %type% * %var% [,)]")) if ((pointer || address) && !alloc && Token::Match(argStart, "const struct| %type% * %var% [,)]"))
return true; return true;
if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]")) if ((pointer || address) && Token::Match(argStart, "const %type% %var% [") && Token::Match(argStart->linkAt(3), "] [,)]"))
return true; return true;

View File

@ -3278,6 +3278,14 @@ private:
" return ab->a;\n" // error " return ab->a;\n" // error
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
checkUninitVar2("struct AB { int a; int b; };\n"
"void do_something(struct AB *ab);\n" // unknown function
"int f() {\n"
" struct AB *ab = malloc(sizeof(struct AB));\n"
" do_something(ab);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void syntax_error() { // Ticket #5073 void syntax_error() { // Ticket #5073