uninitialized variables; fix FP in for loop

This commit is contained in:
Daniel Marjamäki 2021-05-14 21:36:51 +02:00
parent 404b82fc21
commit 809c70b9c3
2 changed files with 39 additions and 0 deletions

View File

@ -836,6 +836,26 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
return nullptr; return nullptr;
} }
// for loop; skip third expression until loop body has been analyzed..
if (tok->str() == ";" && Token::simpleMatch(tok->astParent(), ";") && Token::simpleMatch(tok->astParent()->astParent(), "(")) {
const Token *top = tok->astParent()->astParent();
if (!Token::simpleMatch(top->previous(), "for (") || !Token::simpleMatch(top->link(), ") {"))
continue;
const Token *bodyStart = top->link()->next();
const Token *errorToken = checkLoopBodyRecursive(bodyStart, var, alloc, membervar, bailout);
if (errorToken)
return errorToken;
if (bailout)
return nullptr;
}
// for loop; skip loop body if there is third expression
if (Token::simpleMatch(tok, ") {") &&
Token::simpleMatch(tok->link()->previous(), "for (") &&
Token::simpleMatch(tok->link()->astOperand2(), ";") &&
Token::simpleMatch(tok->link()->astOperand2()->astOperand2(), ";")) {
tok = tok->linkAt(1);
}
if (tok->str() == "{") { if (tok->str() == "{") {
const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout); const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout);
if (Token::simpleMatch(tok->link(), "} else {")) { if (Token::simpleMatch(tok->link(), "} else {")) {

View File

@ -1283,6 +1283,25 @@ private:
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: learn\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: learn\n", errout.str());
checkUninitVar("void foo() {\n"
" Entry *entry, *nextEntry;\n"
" for(int i = 0; i < 10; i++) {\n"
" for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n" // <- nextEntry is not uninitialized
" nextEntry = entry->next;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void foo() {\n"
" Entry *entry, *nextEntry;\n"
" for(int i = 0; i < 10; i++) {\n"
" for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: nextEntry\n", errout.str());
} }
// switch.. // switch..