uninitialized variables; fix false positive in loop

This commit is contained in:
Daniel Marjamäki 2021-05-15 11:16:32 +02:00
parent eb96e4980e
commit 899b88754f
2 changed files with 19 additions and 3 deletions

View File

@ -858,9 +858,11 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
if (tok->str() == "{") {
const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout);
if (Token::simpleMatch(tok->link(), "} else {")) {
const Token *elseBody = tok->link()->tokAt(2);
tok = tok->link();
if (Token::simpleMatch(tok, "} else {")) {
const Token *elseBody = tok->tokAt(2);
const Token *errorToken2 = checkLoopBodyRecursive(elseBody, var, alloc, membervar, bailout);
tok = elseBody->link();
if (errorToken1 && errorToken2)
return errorToken1;
if (errorToken2)
@ -868,7 +870,7 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
}
if (bailout)
return nullptr;
if (errorToken1)
if (!errorToken)
errorToken = errorToken1;
}

View File

@ -1303,6 +1303,20 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: nextEntry\n", errout.str());
checkUninitVar("void f(int x) {\n"
" list *f = NULL;\n"
" list *l;\n"
"\n"
" while (--x) {\n"
" if (!f)\n"
" f = c;\n"
" else\n"
" l->next = c;\n" // <- not uninitialized
" l = c;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// switch..