Skip to content

Commit

Permalink
Fix issue 10166: FP uninitvar with break from infinite loop (#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Feb 11, 2021
1 parent d008356 commit 51f9340
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()) {
Expand Down
53 changes: 53 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4220,6 +4220,59 @@ class TestValueFlow : public TestFixture {
"}";
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() {
Expand Down

0 comments on commit 51f9340

Please sign in to comment.