diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 46330f41e9d..d89229602ee 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -64,9 +64,22 @@ 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); + // A fork carried past an if did not escape, so not every path of the traversal escapes + bool forkFallsThrough = false; + // What the forks that fall through do + Analyzer::Action forkActions = Analyzer::Action::None; + // A path that was not followed to the end might fall through + bool forkMayFallThrough = false; + // End of the whole traversal; a fork inside a branch or loop body only walks to the block end + const Token* traversalEnd = nullptr; + + // The terminate reason is recorded + bool recordsTerminate() const { + return !analyzeOnly || analyzeTerminate; + } Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) { - if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None) + if (recordsTerminate() && t != Analyzer::Terminate::None) terminate = t; return Progress::Break; } @@ -103,6 +116,20 @@ namespace { } }; + // The main path escapes, but if a fork falls through then the traversal as a whole does not + Analyzer::Result result() const { + if (terminate != Analyzer::Terminate::Escape || (!forkFallsThrough && !forkMayFallThrough)) + return Analyzer::Result{actions, terminate}; + // Every path escapes if the range itself ends with an escape + bool unknown = false; + if (Token::simpleMatch(traversalEnd, "}") && traversalEnd->link() && isEscapeScope(traversalEnd, unknown)) + return Analyzer::Result{actions, terminate}; + if (forkFallsThrough) + return Analyzer::Result{actions | forkActions, Analyzer::Terminate::None}; + // It is not known whether the path that was not followed escapes + return Analyzer::Result{actions, Analyzer::Terminate::Bail}; + } + bool stopUpdates() { analyzeOnly = true; return actions.isModified(); @@ -334,6 +361,9 @@ namespace { ft.analyzeTerminate = true; } ft.actions = Analyzer::Action::None; + ft.forkFallsThrough = false; + ft.forkActions = Analyzer::Action::None; + ft.forkMayFallThrough = false; return ft; } @@ -837,7 +867,20 @@ namespace { ++(*forkBudget); ++ft.forkDepth; ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute); - ft.updateRange(tok->next(), end, depth - 1); + const Progress pFork = ft.updateRange(tok->next(), end, depth - 1); + if (end == traversalEnd) { + if (ft.forkFallsThrough || pFork == Progress::Continue || + (ft.recordsTerminate() && ft.terminate == Analyzer::Terminate::Modified)) { + forkFallsThrough = true; + forkActions |= ft.actions | ft.forkActions; + } else if (ft.forkMayFallThrough || !ft.recordsTerminate() || + ft.terminate != Analyzer::Terminate::Escape) { + forkMayFallThrough = true; + } + } + } else if (!thenBranch.isEscape() && end == traversalEnd) { + // The then-path stops in the branch or is not followed + forkMayFallThrough = true; } if (elseBranch.progress == Progress::Break) { // Only the else branch escaped; the then branch falls through, so @@ -994,8 +1037,9 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V ForwardTraversal ft{a, tokenList, errorLogger, settings}; if (start) ft.analyzer->updateState(start); + ft.traversalEnd = end; ft.updateRange(start, end); - return Analyzer::Result{ ft.actions, ft.terminate }; + return ft.result(); } Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& a, const TokenList& tokenList, ErrorLogger& errorLogger, const Settings& settings) @@ -1006,5 +1050,5 @@ Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr& return Analyzer::Result{Analyzer::Action::None, Analyzer::Terminate::Bail}; ForwardTraversal ft{a, tokenList, errorLogger, settings}; (void)ft.updateRecursive(start); - return Analyzer::Result{ ft.actions, ft.terminate }; + return ft.result(); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5961dca21a9..a13e5b44764 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(knownConditionAfterConditionalEscape); // #15264 } struct CheckOptions @@ -6957,6 +6958,121 @@ class TestCondition : public TestFixture { errout_str()); } + void knownConditionAfterConditionalEscape() { + // the then-block falls through when g() and b are true, so 'a' can be true after it + check("bool g();\n" + "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()); + + // the then-path of 'if (g())' stops in its branch, but the then-block falls through when h() is true + check("bool g();\n" + "bool h();\n" + "void use();\n" + "void f(bool a, bool b) {\n" + " if (a) {\n" + " bool w = false;\n" + " if (g()) {\n" + " if (h())\n" + " w = true;\n" + " else\n" + " return;\n" + " }\n" + " if (!w || !b)\n" + " return;\n" + " }\n" + " if (b && a)\n" + " use();\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // every path through the then-block returns + check("bool g();\n" + "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" + " 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()); + + // a fork inside a loop body does not fall through the then-block + check("bool g();\n" + "void use();\n" + "void f(bool a, bool b, int n) {\n" + " if (a) {\n" + " bool z = false;\n" + " for (int i = 0; i < n; i++) {\n" + " if (g())\n" + " use();\n" + " }\n" + " if (!z)\n" + " return;\n" + " }\n" + " if (b && a)\n" + " use();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:10:13]: (style) Condition '!z' is always true [knownConditionTrueFalse]\n" + "[test.cpp:4:9] -> [test.cpp:13:14]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n", + errout_str()); + + // the then-path is not forked when the fork limit is reached, but it falls through + const Settings settingsNormal = settingsBuilder().severity(Severity::style).checkLevel(Settings::CheckLevel::normal).build(); + check("bool g();\n" + "bool h();\n" + "void use();\n" + "void f(bool a, bool b) {\n" + " if (a) {\n" + " bool w = false;\n" + " bool v = false;\n" + " if (g())\n" + " w = true;\n" + " if (h())\n" + " v = true;\n" + " if (!w || !v)\n" + " return;\n" + " }\n" + " if (b && a)\n" + " use();\n" + "}\n", settingsNormal); + ASSERT_EQUALS("", errout_str()); + + // the path that falls through the inner if returns too + check("bool g();\n" + "void use();\n" + "void f(bool a) {\n" + " if (a) {\n" + " bool w = false;\n" + " if (g())\n" + " w = true;\n" + " if (!w)\n" + " return;\n" + " a = false;\n" + " return;\n" + " }\n" + " if (a)\n" + " use();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:13:9]: (style) Condition 'a' is always false [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..e13002ee4fc 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -174,6 +174,7 @@ class TestNullPointer : public TestFixture { TEST_CASE(nullpointerDelete); TEST_CASE(nullpointerSubFunction); TEST_CASE(nullpointerExit); + TEST_CASE(nullpointerAfterConditionalEscape); // #15264 TEST_CASE(nullpointerStdString); TEST_CASE(nullpointerStdStream); TEST_CASE(nullpointerSmartPointer); @@ -4270,6 +4271,25 @@ class TestNullPointer : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void nullpointerAfterConditionalEscape() { + // the then-block falls through when g() is true, so p can be null after it + check("bool g();\n" + "void use(int);\n" + "void f(int *p) {\n" + " if (!p) {\n" + " bool w = false;\n" + " if (g())\n" + " w = true;\n" + " if (!w)\n" + " return;\n" + " }\n" + " use(*p);\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:4:9] -> [test.cpp:11:10]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n", + errout_str()); + } + void nullpointerStdString() { check("void f(std::string s1) {\n" " void* p = 0;\n" diff --git a/test/testother.cpp b/test/testother.cpp index 085b39cfffb..49ff5b4cab6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -936,6 +936,77 @@ class TestOther : public TestFixture { " return 0;\n" "}\n"); ASSERT_EQUALS("[test.cpp:4:26]: (warning) Division by zero. [zerodivcond]\n", errout_str()); + + // the path that falls through the then-block assigns a (#15264) + check("bool g();\n" + "int f(int a) {\n" + " if (a == 0) {\n" + " bool w = false;\n" + " if (g())\n" + " w = true;\n" + " if (!w)\n" + " return 0;\n" + " a = 1;\n" + " }\n" + " return 10 / a;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("bool g();\n" + "int f(int a, int n) {\n" + " if (a == 0) {\n" + " for (int i = 0; i < n; i++) {\n" + " bool w = false;\n" + " if (g())\n" + " w = true;\n" + " if (!w)\n" + " return 0;\n" + " }\n" + " a = 1;\n" + " }\n" + " return 10 / a;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + // the then-paths that are not followed at the normal check level return as well + Settings settingsNormal = settingsBuilder(settings1).checkLevel(Settings::CheckLevel::normal).build(); + check("bool g();\n" + "bool h();\n" + "void use(int);\n" + "int f(int x) {\n" + " if (x == 0) {\n" + " bool w = true;\n" + " if (g())\n" + " use(1);\n" + " if (h())\n" + " use(2);\n" + " if (w) {\n" + " use(3);\n" + " return 0;\n" + " }\n" + " }\n" + " return 10 / x;\n" + "}\n", dinit(CheckOptions, $.settings = &settingsNormal)); + ASSERT_EQUALS("", errout_str()); + + // a fork that stops at the branch limit does not fall through with the assignment + check("bool g(int);\n" + "void use(int);\n" + "int f(int x) {\n" + " if (x == 0) {\n" + " bool w = false;\n" + " if (g(0))\n" + " w = true;\n" + " if (!w)\n" + " return 0;\n" + " if (g(1)) use(1);\n" + " if (g(2)) use(2);\n" + " if (g(3)) use(3);\n" + " x = 1;\n" + " }\n" + " return 10 / x;\n" + "}\n", dinit(CheckOptions, $.settings = &settingsNormal)); + ASSERT_EQUALS("", errout_str()); } void zeroDivErrorPath() {