Fix 9772: FP uninitvar: in cppcheck 2.1 (#3638)

This commit is contained in:
Paul Fultz II 2021-12-17 14:51:47 -06:00 committed by GitHub
parent c918e1bc50
commit 332f4c205a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -408,8 +408,16 @@ struct ForwardTraversal {
bool checkElse = false; bool checkElse = false;
if (condTok && !Token::simpleMatch(condTok, ":")) if (condTok && !Token::simpleMatch(condTok, ":"))
std::tie(checkThen, checkElse) = evalCond(condTok, isDoWhile ? endBlock->previous() : nullptr); std::tie(checkThen, checkElse) = evalCond(condTok, isDoWhile ? endBlock->previous() : nullptr);
if (checkElse && exit && !hasJump(endBlock)) // exiting a do while(false)
if (checkElse && exit) {
if (hasJump(endBlock)) {
if (!analyzer->lowerToPossible())
return Break(Analyzer::Terminate::Bail);
if (analyzer->isConditional() && stopUpdates())
return Break(Analyzer::Terminate::Conditional);
}
return Progress::Continue; return Progress::Continue;
}
Analyzer::Action bodyAnalysis = analyzeScope(endBlock); Analyzer::Action bodyAnalysis = analyzeScope(endBlock);
Analyzer::Action allAnalysis = bodyAnalysis; Analyzer::Action allAnalysis = bodyAnalysis;
Analyzer::Action condAnalysis; Analyzer::Action condAnalysis;
@ -421,7 +429,7 @@ struct ForwardTraversal {
allAnalysis |= analyzeRecursive(stepTok); allAnalysis |= analyzeRecursive(stepTok);
actions |= allAnalysis; actions |= allAnalysis;
// do while(false) is not really a loop // do while(false) is not really a loop
if (checkElse && isDoWhile && !hasJump(endBlock) && if (checkElse && isDoWhile &&
(condTok->hasKnownIntValue() || (condTok->hasKnownIntValue() ||
(!bodyAnalysis.isModified() && !condAnalysis.isModified() && condAnalysis.isRead()))) { (!bodyAnalysis.isModified() && !condAnalysis.isModified() && condAnalysis.isRead()))) {
if (updateRange(endBlock->link(), endBlock) == Progress::Break) if (updateRange(endBlock->link(), endBlock) == Progress::Break)

View File

@ -5112,6 +5112,23 @@ private:
" return -1;\n" " return -1;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
// #9772
valueFlowUninit("int func(void) {\n"
" int rez;\n"
" struct sccb* ccb;\n"
" do {\n"
" if ((ccb = calloc(1, sizeof(*ccb))) == NULL) {\n"
" rez = 1;\n"
" break;\n"
" }\n"
" rez = 0;\n"
" } while (0);\n"
" if (rez != 0)\n"
" free(ccb);\n"
" return rez;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value