Skip to content

Commit 449deda

Browse files
committed
Track the other paths
1 parent 35e76b5 commit 449deda

2 files changed

Lines changed: 144 additions & 1 deletion

File tree

‎lib/forwardanalyzer.cpp‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,38 @@ namespace {
6565
// Total forks of the traversal (shared via the fork() copy); backstop past the depth bound.
6666
std::shared_ptr<int> forkBudget = std::make_shared<int>(0);
6767

68+
// The paths that left this traversal without escaping: forks carried past their if and
69+
// then-paths that are not followed further.
70+
struct OtherPaths {
71+
Analyzer::Result result;
72+
bool exist = false;
73+
void add(const Analyzer::Result& r) {
74+
if (r.terminate == Analyzer::Terminate::Escape)
75+
return;
76+
result.update(r);
77+
exist = true;
78+
}
79+
void add(const OtherPaths& paths) {
80+
if (paths.exist)
81+
add(paths.result);
82+
}
83+
};
84+
OtherPaths otherPaths;
85+
86+
// The result over every path to end, not only the path this traversal follows
87+
Analyzer::Result result(const Token* end) const {
88+
Analyzer::Result r{actions, terminate};
89+
if (!otherPaths.exist)
90+
return r;
91+
if (terminate == Analyzer::Terminate::Escape) {
92+
if (isReturnScope(end, settings().library))
93+
return r;
94+
r.terminate = otherPaths.result.terminate;
95+
}
96+
r.action |= otherPaths.result.action;
97+
return r;
98+
}
99+
68100
Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) {
69101
if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None)
70102
terminate = t;
@@ -334,6 +366,7 @@ namespace {
334366
ft.analyzeTerminate = true;
335367
}
336368
ft.actions = Analyzer::Action::None;
369+
ft.otherPaths = OtherPaths{};
337370
return ft;
338371
}
339372

@@ -407,6 +440,8 @@ namespace {
407440
// Save and reset actions
408441
Analyzer::Action prevActions = actions;
409442
actions = Analyzer::Action::None;
443+
OtherPaths prevPaths = otherPaths;
444+
otherPaths = OtherPaths{};
410445
Progress p = updateRange(branch.endBlock->link(), branch.endBlock, depth);
411446
branch.action |= actions;
412447
// Restore actions
@@ -434,6 +469,14 @@ namespace {
434469
}
435470
}
436471

472+
// The other paths escape as well when the scope always escapes
473+
if (!branch.isConclusiveEscape()) {
474+
if (otherPaths.exist)
475+
branch.action |= otherPaths.result.action;
476+
prevPaths.add(otherPaths);
477+
}
478+
otherPaths = prevPaths;
479+
437480
return p;
438481
}
439482

@@ -838,6 +881,9 @@ namespace {
838881
++ft.forkDepth;
839882
ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
840883
ft.updateRange(tok->next(), end, depth - 1);
884+
otherPaths.add(ft.result(end));
885+
} else if (!thenBranch.isConclusiveEscape()) {
886+
otherPaths.add(ft.result(thenBranch.endBlock));
841887
}
842888
if (elseBranch.progress == Progress::Break) {
843889
// Only the else branch escaped; the then branch falls through, so
@@ -995,7 +1041,7 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V
9951041
if (start)
9961042
ft.analyzer->updateState(start);
9971043
ft.updateRange(start, end);
998-
return Analyzer::Result{ ft.actions, ft.terminate };
1044+
return ft.result(end);
9991045
}
10001046

10011047
Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>& a, const TokenList& tokenList, ErrorLogger& errorLogger, const Settings& settings)

‎test/testcondition.cpp‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5005,6 +5005,103 @@ class TestCondition : public TestFixture {
50055005
"}\n");
50065006
ASSERT_EQUALS("", errout_str());
50075007

5008+
check("bool g();\n" // only the path that skips 'w = true' returns
5009+
"void use();\n"
5010+
"void f(bool a, bool b) {\n"
5011+
" if (a) {\n"
5012+
" bool w = false;\n"
5013+
" if (g())\n"
5014+
" w = true;\n"
5015+
" if (!w || !b)\n"
5016+
" return;\n"
5017+
" }\n"
5018+
" if (b && a)\n"
5019+
" use();\n"
5020+
"}\n");
5021+
ASSERT_EQUALS("", errout_str());
5022+
5023+
check("bool g();\n" // the path is split inside a block that is always entered
5024+
"void use();\n"
5025+
"void f(bool a, bool b) {\n"
5026+
" if (a) {\n"
5027+
" bool w = false;\n"
5028+
" if (a) {\n"
5029+
" if (g())\n"
5030+
" w = true;\n"
5031+
" }\n"
5032+
" if (!w)\n"
5033+
" return;\n"
5034+
" }\n"
5035+
" if (b && a)\n"
5036+
" use();\n"
5037+
"}\n");
5038+
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:6:13]: (warning) Identical inner 'if' condition is always true. [identicalInnerCondition]\n",
5039+
errout_str());
5040+
5041+
check("bool g();\n" // x is assigned on the path that sets w
5042+
"void use();\n"
5043+
"void f(int x) {\n"
5044+
" if (x == 0) {\n"
5045+
" bool w = false;\n"
5046+
" if (g())\n"
5047+
" w = true;\n"
5048+
" if (w)\n"
5049+
" x = 1;\n"
5050+
" } else {\n"
5051+
" return;\n"
5052+
" }\n"
5053+
" if (x == 0)\n"
5054+
" use();\n"
5055+
"}\n");
5056+
ASSERT_EQUALS("", errout_str());
5057+
5058+
check("bool g();\n"
5059+
"void use();\n"
5060+
"void f(bool c) {\n"
5061+
" int x = 0;\n"
5062+
" if (c) {\n"
5063+
" bool w = false;\n"
5064+
" if (g())\n"
5065+
" w = true;\n"
5066+
" if (w)\n"
5067+
" x = 1;\n"
5068+
" }\n"
5069+
" if (x == 0)\n"
5070+
" use();\n"
5071+
"}\n");
5072+
ASSERT_EQUALS("", errout_str());
5073+
5074+
check("bool g();\n" // every path returns
5075+
"void use();\n"
5076+
"void f(bool a, bool b) {\n"
5077+
" if (a) {\n"
5078+
" bool w = false;\n"
5079+
" if (g())\n"
5080+
" w = true;\n"
5081+
" if (!w)\n"
5082+
" return;\n"
5083+
" return;\n"
5084+
" }\n"
5085+
" if (b && a)\n"
5086+
" use();\n"
5087+
"}\n");
5088+
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:12:14]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n",
5089+
errout_str());
5090+
5091+
check("bool g();\n"
5092+
"void use();\n"
5093+
"void f(int x) {\n"
5094+
" if (x == 0) {\n"
5095+
" if (g())\n"
5096+
" x = 1;\n"
5097+
" return;\n"
5098+
" }\n"
5099+
" if (x == 0)\n"
5100+
" use();\n"
5101+
"}\n");
5102+
ASSERT_EQUALS("[test.cpp:4:11] -> [test.cpp:9:11]: (style) Condition 'x==0' is always false [knownConditionTrueFalse]\n",
5103+
errout_str());
5104+
50085105
check("void f() {\n"
50095106
" if (42) {}\n"
50105107
" if (42U) {}\n"

0 commit comments

Comments
 (0)