diff --git a/lib/analyzer.h b/lib/analyzer.h index f2b7460d7cb..dea535a84d4 100644 --- a/lib/analyzer.h +++ b/lib/analyzer.h @@ -189,6 +189,10 @@ struct Analyzer { virtual void assume(const Token* tok, bool state, unsigned int flags = 0) = 0; /// Update the state of the program at the token virtual void updateState(const Token* tok) = 0; + /// Go back to the program state of 'before' (an earlier copy of this analyzer) and update it at the token + virtual void resetState(const Analyzer& /*before*/, const Token* tok) { + updateState(tok); + } /// Return analyzer for expression at token virtual ValuePtr reanalyze(Token* tok, const std::string& msg = "") const = 0; virtual bool invalid() const { diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 46330f41e9d..c78f5373c1d 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -64,6 +64,10 @@ namespace { int forkDepth = 0; // Total forks of the traversal (shared via the fork() copy); backstop past the depth bound. std::shared_ptr forkBudget = std::make_shared(0); + // End of the whole traversal; a fork inside a branch or loop body only walks to the block end + const Token* traversalEnd = nullptr; + // A then-path carried past an if in the current block reached the block end and ends there + bool forkLostAtBlockEnd = false; Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) { if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None) @@ -329,6 +333,7 @@ namespace { ForwardTraversal fork(bool analyze = false) const { ForwardTraversal ft = *this; + ft.forkLostAtBlockEnd = false; if (analyze) { ft.analyzeOnly = true; ft.analyzeTerminate = true; @@ -402,12 +407,27 @@ namespace { return a; } + // A then-path carried past an if inside the block ends at the block end. This traversal assumed + // that path's condition false, so after the block it has to stand for that path as well: what it + // learned in the block is dropped again. + Progress updateBlock(Token* endBlock, int depth) + { + const ValuePtr before = analyzer; + const bool outerForkLost = forkLostAtBlockEnd; + forkLostAtBlockEnd = false; + const Progress p = updateRange(endBlock->link(), endBlock, depth); + if (forkLostAtBlockEnd && p != Progress::Break) + analyzer->resetState(*before, endBlock); + forkLostAtBlockEnd = outerForkLost; + return p; + } + Progress updateBranch(Branch& branch, int depth) { // Save and reset actions Analyzer::Action prevActions = actions; actions = Analyzer::Action::None; - Progress p = updateRange(branch.endBlock->link(), branch.endBlock, depth); + Progress p = updateBlock(branch.endBlock, depth); branch.action |= actions; // Restore actions actions |= prevActions; @@ -624,7 +644,7 @@ namespace { if (!endBlock) return Break(); updateScopeState(endBlock); - return updateRange(endBlock->link(), endBlock, depth); + return updateBlock(endBlock, depth); } /** @@ -837,7 +857,10 @@ namespace { ++(*forkBudget); ++ft.forkDepth; ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute); - ft.updateRange(tok->next(), end, depth - 1); + const Progress pCarried = ft.updateRange(tok->next(), end, depth - 1); + // Inside a branch or loop body the fork walks only to the block end + if (pCarried != Progress::Break && end != traversalEnd) + forkLostAtBlockEnd = true; } if (elseBranch.progress == Progress::Break) { // Only the else branch escaped; the then branch falls through, so @@ -992,6 +1015,7 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V if (a->invalid()) return Analyzer::Result{Analyzer::Action::None, Analyzer::Terminate::Bail}; ForwardTraversal ft{a, tokenList, errorLogger, settings}; + ft.traversalEnd = end; if (start) ft.analyzer->updateState(start); ft.updateRange(start, end); diff --git a/lib/vf_analyzers.cpp b/lib/vf_analyzers.cpp index ccadc3ef623..545835de808 100644 --- a/lib/vf_analyzers.cpp +++ b/lib/vf_analyzers.cpp @@ -815,6 +815,14 @@ struct ValueFlowAnalyzer : Analyzer { pms.addState(tok, getProgramState()); } + void resetState(const Analyzer& before, const Token* tok) override + { + // 'before' is a copy of this analyzer + assert(dynamic_cast(&before)); + pms = static_cast(before).pms; + updateState(tok); + } + void update(Token* tok, Action a, Direction d) override { ValueFlow::Value* value = getValue(tok); if (!value) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5961dca21a9..291bf6ab729 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -131,6 +131,7 @@ class TestCondition : public TestFixture { TEST_CASE(knownConditionIncDecOperator); TEST_CASE(knownConditionFloating); TEST_CASE(knownConditionRecordedComparison); + TEST_CASE(knownConditionFlagSetInNestedBranch); } struct CheckOptions @@ -6957,6 +6958,27 @@ class TestCondition : public TestFixture { errout_str()); } + void knownConditionFlagSetInNestedBranch() { + check("bool g();\n" // #15266 + "bool h();\n" + "int send();\n" + "int f() {\n" + " int x = 0;\n" + " bool b = false;\n" + " if (g()) {\n" + " if (h())\n" + " b = true;\n" + " }\n" + " if (b) {\n" + " if (x == 0)\n" + " x = send();\n" + " }\n" + " return x;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:12:15]: (style) Condition 'x==0' is always true [knownConditionTrueFalse]\n", + errout_str()); + } + // TODO: a comparison of two expressions without values should decide the comparisons of the pair // that move a constant to the other side: "(x + 1) < w[0]" is "x < w[0] - 1". At the normal check // level the forward analysis forks on a few unknown conditions only, so the condition has to be diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index cecbb27ca09..2644edfcd2d 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3296,6 +3296,32 @@ class TestNullPointer : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); + // inside a block that does not return + check("void g(int);\n" + "void f(int x, int c) {\n" + " int *p = 0;\n" + " if (c) {\n" + " if (x > 5) x = 5;\n" + " if (x > 5) g(*p);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // the flag is set in a nested branch + check("bool g();\n" // #15266 + "int f(int c) {\n" + " int *p = 0;\n" + " bool b = false;\n" + " if (c) {\n" + " if (g())\n" + " b = true;\n" + " }\n" + " if (b)\n" + " return *p;\n" + " return 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:10:17]: (error) Null pointer dereference: p [nullPointer]\n", errout_str()); + // false on both paths check("int f(int x) {\n" " int *p = 0;\n" diff --git a/test/testother.cpp b/test/testother.cpp index 085b39cfffb..f090ccfb648 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -909,6 +909,19 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); + // inside a block that does not return + check("void g(int);\n" + "int f(int x) {\n" + " int d = 0;\n" + " if (x >= 0) {\n" + " if (x > 5) x = 0;\n" + " if (x <= 5) d = 1;\n" + " g(10 / d);\n" + " }\n" + " return d;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + check("int f(int x) {\n" " int d = 0;\n" " if (x >= 0) {\n" diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 787ede556ea..045b2e947af 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -7976,6 +7976,20 @@ class TestUninitVar : public TestFixture { " *v[0] = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + valueFlowUninit("struct D { int a; int b; };\n" // #15266 + "void use(D d);\n" + "void f(const int* p, int n) {\n" + " D d;\n" + " d.a = 0;\n" + " bool found = false;\n" + " for (int i = 0; i < n; i++)\n" + " if (p[i] == 0)\n" + " found = true;\n" + " if (found)\n" + " use(d);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:10:9] -> [test.cpp:11:13]: (warning) Uninitialized variable: d.b [uninitvar]\n", errout_str()); } void valueFlowUninitForLoop() diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index dfbb98ad72c..496756fd7e4 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -4674,6 +4674,32 @@ class TestValueFlow : public TestFixture { "}\n"; ASSERT_EQUALS(false, testValueOfX(code, 5U, 0)); + // the fork that sets the flag in a nested branch ends with the outer block, so the main path + // must not drop it: the value still reaches the branch on the flag + code = "bool g();\n" + "bool h();\n" + "void f() {\n" + " int x = 1;\n" + " bool b = false;\n" + " if (g()) {\n" + " if (h())\n" + " b = true;\n" + " }\n" + " if (b) { int a = x; }\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 10U, 1)); + + // the same with the flag set in a loop + code = "void f(const int* p, int n) {\n" + " int x = 1;\n" + " bool b = false;\n" + " for (int i = 0; i < n; i++)\n" + " if (p[i] == 0)\n" + " b = true;\n" + " if (b) { int a = x; }\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 7U, 1)); + // no fork: the main path still knows the condition is false /*const*/ Settings s(settings); s.vfOptions.maxForwardConditionForkDepth = 0;