Fix issue 10166: FP uninitvar with break from infinite loop (#3124)

This commit is contained in:
Paul Fultz II 2021-02-11 01:07:12 -06:00 committed by GitHub
parent d008356c28
commit 51f93400d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

@ -329,6 +329,7 @@ struct ForwardTraversal {
Token* initTok = nullptr,
Token* stepTok = nullptr) {
const bool isDoWhile = precedes(endBlock, condTok);
const bool alwaysEnterLoop = !condTok || (condTok->hasKnownIntValue() && condTok->values().front().intvalue != 0);
Analyzer::Action bodyAnalysis = analyzeScope(endBlock);
Analyzer::Action allAnalysis = bodyAnalysis;
if (condTok)
@ -357,6 +358,8 @@ struct ForwardTraversal {
if (checkElse)
return Progress::Continue;
}
if (allAnalysis.isModified() && alwaysEnterLoop)
return Break(Terminate::Bail);
std::vector<ForwardTraversal> ftv = tryForkScope(endBlock, allAnalysis.isModified());
if (bodyAnalysis.isModified()) {

View File

@ -4220,6 +4220,59 @@ private:
"}";
values = tokenValues(code, "x . value");
ASSERT_EQUALS(0, values.size());
// #10166
code = "int f(bool b) {\n"
" int x;\n"
" do {\n"
" if (b) {\n"
" x = 0;\n"
" break;\n"
" }\n"
" } while (true);\n"
" return x;\n"
"}\n";
values = tokenValues(code, "x ; }", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
code = "int f(bool b) {\n"
" int x;\n"
" while (true) {\n"
" if (b) {\n"
" x = 0;\n"
" break;\n"
" }\n"
" }\n"
" return x;\n"
"}\n";
values = tokenValues(code, "x ; }", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
code = "int f(bool b) {\n"
" int x;\n"
" for(;;) {\n"
" if (b) {\n"
" x = 0;\n"
" break;\n"
" }\n"
" }\n"
" return x;\n"
"}\n";
values = tokenValues(code, "x ; }", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
code = "int f(bool b) {\n"
" int x;\n"
" switch (b) {\n"
" case 1: {\n"
" ret = 0;\n"
" break;\n"
" }\n"
" }\n"
" return x;\n"
"}\n";
values = tokenValues(code, "x ; }", ValueFlow::Value::ValueType::UNINIT);
ASSERT_EQUALS(0, values.size());
}
void valueFlowConditionExpressions() {