Fixed #4970 (false positive: conditionally initialized variable used in if-clause (git/notes.c))

This commit is contained in:
Daniel Marjamäki 2014-11-01 14:03:02 +01:00
parent c7d315fba3
commit b766071272
2 changed files with 10 additions and 1 deletions

View File

@ -1525,6 +1525,8 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok,
bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, bool alloc, const std::string &membervar)
{
const Token * const endpar = startparentheses->link();
if (Token::Match(startparentheses, "( ! %var% %oror%") && startparentheses->tokAt(2)->getValue(0))
suppressErrors = true;
for (const Token *tok = startparentheses->next(); tok && tok != endpar; tok = tok->next()) {
if (tok->varId() == var.declarationId()) {
if (Token::Match(tok, "%var% . %var%")) {

View File

@ -2405,6 +2405,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("static void f() {\n"
" int a=0, b;\n"
" if (something) { a = dostuff(&b); }\n"
" if (!a || b) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar2("static void f(int x, int y) {\n"
" int a;\n"
" if (x == 0 && (a == 1)) { }\n"
@ -2798,7 +2805,7 @@ private:
" if (!x) i = 0;\n"
" if (!x || i>0) {}\n" // <- error
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", "", errout.str());
checkUninitVar2("void f(int x) {\n"
" int i;\n"