Uninitialized variables: Fixed false negative in new checking when using while loops

This commit is contained in:
Daniel Marjamäki 2012-11-30 06:30:04 +01:00
parent 35c2b8058d
commit 82223227bd
2 changed files with 10 additions and 2 deletions

View File

@ -1197,8 +1197,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) if (Token::Match(tok, "sizeof|typeof|offsetof|decltype ("))
tok = tok->next()->link(); tok = tok->next()->link();
// for.. // for/while..
if (Token::simpleMatch(tok, "for (")) { if (Token::Match(tok, "for|while (")) {
// is variable initialized in for-head (don't report errors yet)? // is variable initialized in for-head (don't report errors yet)?
if (checkIfForWhileHead(tok->next(), var, true, false)) if (checkIfForWhileHead(tok->next(), var, true, false))
return true; return true;

View File

@ -2116,6 +2116,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("int f() {\n"
" int x;\n"
" while (foo)\n"
" if (bar) break;\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
// if goto is simplified there might be conditions that are always true // if goto is simplified there might be conditions that are always true
checkUninitVar2("void f() {\n" checkUninitVar2("void f() {\n"
" int i;\n" " int i;\n"