Skip to content
Open
15 changes: 13 additions & 2 deletions lib/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ struct Analyzer {
unsigned int mFlag{};
};

enum class Terminate : std::uint8_t { None, Bail, Escape, Modified, Inconclusive, Conditional };
enum class Terminate : std::uint8_t {
None,
Bail,
Escape,
Modified,
Inconclusive,
Conditional,
// Not all paths terminate
Partial
};

struct Result {
explicit Result(Action action = Action::None, Terminate terminate = Terminate::None)
Expand All @@ -142,7 +151,9 @@ struct Analyzer {
Terminate terminate;

void update(Result rhs) {
if (terminate == Terminate::None)
// A value that stopped on every path decides over one that reached the end on some
const bool reached = terminate == Terminate::None || terminate == Terminate::Partial;
if (reached && rhs.terminate != Terminate::None)
terminate = rhs.terminate;
action |= rhs.action;
}
Expand Down
464 changes: 359 additions & 105 deletions lib/forwardanalyzer.cpp

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4867,7 +4867,7 @@ struct ConditionHandler {
}
}

bool deadBranch[] = {false, false};
Analyzer::Result results[2];
// start token of conditional code
Token* startTokens[] = {nullptr, nullptr};
// determine startToken(s)
Expand All @@ -4887,14 +4887,17 @@ struct ConditionHandler {
if (allowKnown)
valueFlowSetConditionToKnown(condTok, values, i == 0);

Analyzer::Result r = forward(startTokens[i], startTokens[i]->link(), cond.vartok, values, tokenlist, errorLogger, settings);
deadBranch[i] = r.terminate == Analyzer::Terminate::Escape;
if (r.action.isModified() && !deadBranch[i])
changeBlock = i;
if (r.terminate != Analyzer::Terminate::None && r.terminate != Analyzer::Terminate::Escape &&
r.terminate != Analyzer::Terminate::Modified)
bailBlock = i;
results[i] = forward(startTokens[i], startTokens[i]->link(), cond.vartok, values, tokenlist, errorLogger, settings);
const Analyzer::Terminate terminate = results[i].terminate;
if (terminate != Analyzer::Terminate::Escape && terminate != Analyzer::Terminate::Partial) {
if (results[i].action.isModified())
changeBlock = i;
if (terminate != Analyzer::Terminate::None && terminate != Analyzer::Terminate::Modified)
bailBlock = i;
}
changeKnownToPossible(values);
if (terminate == Analyzer::Terminate::Partial)
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
}
if (changeBlock >= 0 && !Token::simpleMatch(top->previous(), "while (")) {
if (settings.debugwarnings)
Expand All @@ -4917,8 +4920,8 @@ struct ConditionHandler {
// After conditional code..
if (Token::simpleMatch(top->link(), ") {")) {
Token* after = top->link()->linkAt(1);
bool dead_if = deadBranch[0];
bool dead_else = deadBranch[1];
bool dead_if = results[0].terminate == Analyzer::Terminate::Escape;
bool dead_else = results[1].terminate == Analyzer::Terminate::Escape;
const Token* unknownFunction = nullptr;
if (condTok->astParent() && Token::Match(top->previous(), "while|for ("))
dead_if = !isBreakScope(after);
Expand Down Expand Up @@ -4990,7 +4993,7 @@ struct ConditionHandler {
if (possible) {
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
changeKnownToPossible(values);
} else if (allowKnown) {
} else if (allowKnown && results[dead_if ? 1 : 0].terminate != Analyzer::Terminate::Partial) {
valueFlowSetConditionToKnown(condTok, values, true);
valueFlowSetConditionToKnown(condTok, values, false);
}
Expand Down
256 changes: 256 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5005,6 +5005,262 @@ class TestCondition : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // only the path that skips 'w = true' returns
"void use();\n"
"void f(bool a, bool b) {\n"
" if (a) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w || !b)\n"
" return;\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // the path is split inside a block that is always entered
"void use();\n"
"void f(bool a, bool b) {\n"
" if (a) {\n"
" bool w = false;\n"
" if (a) {\n"
" if (g())\n"
" w = true;\n"
" }\n"
" if (!w)\n"
" return;\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:6:13]: (warning) Identical inner 'if' condition is always true. [identicalInnerCondition]\n",
errout_str());

check("bool g();\n" // x is assigned on the path that sets w
"void use();\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (w)\n"
" x = 1;\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n"
"void use();\n"
"void f(bool c) {\n"
" int x = 0;\n"
" if (c) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (w)\n"
" x = 1;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // every path returns
"void use();\n"
"void f(bool a, bool b) {\n"
" if (a) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w)\n"
" return;\n"
" return;\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:12:14]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n",
errout_str());

check("bool g();\n"
"void use();\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" if (g())\n"
" x = 1;\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:11] -> [test.cpp:9:11]: (style) Condition 'x==0' is always false [knownConditionTrueFalse]\n",
errout_str());

check("bool g();\n" // the path that leaves the else block assigns x afterwards
"bool c();\n"
"int h();\n"
"void use(int);\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" if (c()) {\n"
" } else {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w)\n"
" return;\n"
" }\n"
" x = h();\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use(x);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // the same in a block that is always entered
"int h();\n"
"void use(int);\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" if (x == 0) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w)\n"
" return;\n"
" }\n"
" x = h();\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use(x);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:11] -> [test.cpp:6:15]: (warning) Identical inner 'if' condition is always true. [identicalInnerCondition]\n",
errout_str());

check("bool g();\n" // #15264 - the split is in the then block of another unknown condition
"bool h();\n"
"void use();\n"
"void f(bool a, bool b) {\n"
" if (a) {\n"
" if (h()) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w)\n"
" return;\n"
" } else {\n"
" return;\n"
" }\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // #15264 - the split is in a loop
"void use();\n"
"void f(bool a, bool b, bool c) {\n"
" if (a) {\n"
" bool w = false;\n"
" while (g()) {\n"
" if (c)\n"
" w = true;\n"
" }\n"
" if (!w)\n"
" return;\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // the path that leaves the else block assigns x in a later block
"bool c();\n"
"void use();\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" bool w = false;\n"
" if (c()) {\n"
" } else {\n"
" if (g())\n"
" w = true;\n"
" }\n"
" if (w)\n"
" x = 1;\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // the path that leaves the loop assigns x in a later block
"void use();\n"
"void f(int x, bool c) {\n"
" if (x == 0) {\n"
" bool w = false;\n"
" while (g()) {\n"
" if (c)\n"
" w = true;\n"
" }\n"
" if (w)\n"
" x = 1;\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

{
// Without splitting, the path past 'if (g())' stands for both outcomes
/*const*/ Settings s = settings0;
s.vfOptions.maxForwardConditionForkDepth = 0;
check("bool g();\n"
"void use();\n"
"void f(int x) {\n"
" if (x == 0) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (w)\n"
" x = 1;\n"
" } else {\n"
" return;\n"
" }\n"
" if (x == 0)\n"
" use();\n"
"}\n", s);
ASSERT_EQUALS("", errout_str());

check("bool g();\n" // #15264
"void use();\n"
"void f(bool a, bool b) {\n"
" if (a) {\n"
" bool w = false;\n"
" if (g())\n"
" w = true;\n"
" if (!w || !b)\n"
" return;\n"
" }\n"
" if (b && a)\n"
" use();\n"
"}\n", s);
ASSERT_EQUALS("", errout_str());
}

check("void f() {\n"
" if (42) {}\n"
" if (42U) {}\n"
Expand Down
Loading
Loading