Skip to content

Commit 6a457d6

Browse files
autoantwortclaude
andcommitted
Fix #15264 FP knownConditionTrueFalse: then-block treated as always escaping when only one path through a nested if returns
The forward traversal carries the true path of an opaque condition past the if on a fork. Since 4b66976 the main traversal also commits to the false path (absolute assumption) when the then-branch falls through. When the main path later escapes, the traversal reported Terminate::Escape although the fork fell through, so afterCondition() considered the block dead. A fork that is carried to the end of the whole traversal, or stops because the value is modified, now makes the result fall through, together with its actions. A fork that stops for another reason, or a then-path that stops in the branch or is not followed (fork limits), is unknown: if the main path escapes, the result is Terminate::Bail. The result stays Terminate::Escape if the traversed range ends with an escape. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 61d2460 commit 6a457d6

4 files changed

Lines changed: 255 additions & 4 deletions

File tree

‎lib/forwardanalyzer.cpp‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,22 @@ namespace {
6464
int forkDepth = 0;
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);
67+
// A fork carried past an if did not escape, so not every path of the traversal escapes
68+
bool forkFallsThrough = false;
69+
// What the forks that fall through do
70+
Analyzer::Action forkActions = Analyzer::Action::None;
71+
// A path that was not followed to the end might fall through
72+
bool forkMayFallThrough = false;
73+
// End of the whole traversal; a fork inside a branch or loop body only walks to the block end
74+
const Token* traversalEnd = nullptr;
75+
76+
// The terminate reason is recorded
77+
bool recordsTerminate() const {
78+
return !analyzeOnly || analyzeTerminate;
79+
}
6780

6881
Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) {
69-
if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None)
82+
if (recordsTerminate() && t != Analyzer::Terminate::None)
7083
terminate = t;
7184
return Progress::Break;
7285
}
@@ -103,6 +116,20 @@ namespace {
103116
}
104117
};
105118

119+
// The main path escapes, but if a fork falls through then the traversal as a whole does not
120+
Analyzer::Result result() const {
121+
if (terminate != Analyzer::Terminate::Escape || (!forkFallsThrough && !forkMayFallThrough))
122+
return Analyzer::Result{actions, terminate};
123+
// Every path escapes if the range itself ends with an escape
124+
bool unknown = false;
125+
if (Token::simpleMatch(traversalEnd, "}") && traversalEnd->link() && isEscapeScope(traversalEnd, unknown))
126+
return Analyzer::Result{actions, terminate};
127+
if (forkFallsThrough)
128+
return Analyzer::Result{actions | forkActions, Analyzer::Terminate::None};
129+
// It is not known whether the path that was not followed escapes
130+
return Analyzer::Result{actions, Analyzer::Terminate::Bail};
131+
}
132+
106133
bool stopUpdates() {
107134
analyzeOnly = true;
108135
return actions.isModified();
@@ -334,6 +361,9 @@ namespace {
334361
ft.analyzeTerminate = true;
335362
}
336363
ft.actions = Analyzer::Action::None;
364+
ft.forkFallsThrough = false;
365+
ft.forkActions = Analyzer::Action::None;
366+
ft.forkMayFallThrough = false;
337367
return ft;
338368
}
339369

@@ -837,7 +867,20 @@ namespace {
837867
++(*forkBudget);
838868
++ft.forkDepth;
839869
ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
840-
ft.updateRange(tok->next(), end, depth - 1);
870+
const Progress pFork = ft.updateRange(tok->next(), end, depth - 1);
871+
if (end == traversalEnd) {
872+
if (ft.forkFallsThrough || pFork == Progress::Continue ||
873+
(ft.recordsTerminate() && ft.terminate == Analyzer::Terminate::Modified)) {
874+
forkFallsThrough = true;
875+
forkActions |= ft.actions | ft.forkActions;
876+
} else if (ft.forkMayFallThrough || !ft.recordsTerminate() ||
877+
ft.terminate != Analyzer::Terminate::Escape) {
878+
forkMayFallThrough = true;
879+
}
880+
}
881+
} else if (!thenBranch.isEscape() && end == traversalEnd) {
882+
// The then-path stops in the branch or is not followed
883+
forkMayFallThrough = true;
841884
}
842885
if (elseBranch.progress == Progress::Break) {
843886
// 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
9941037
ForwardTraversal ft{a, tokenList, errorLogger, settings};
9951038
if (start)
9961039
ft.analyzer->updateState(start);
1040+
ft.traversalEnd = end;
9971041
ft.updateRange(start, end);
998-
return Analyzer::Result{ ft.actions, ft.terminate };
1042+
return ft.result();
9991043
}
10001044

10011045
Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>& a, const TokenList& tokenList, ErrorLogger& errorLogger, const Settings& settings)
@@ -1006,5 +1050,5 @@ Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>&
10061050
return Analyzer::Result{Analyzer::Action::None, Analyzer::Terminate::Bail};
10071051
ForwardTraversal ft{a, tokenList, errorLogger, settings};
10081052
(void)ft.updateRecursive(start);
1009-
return Analyzer::Result{ ft.actions, ft.terminate };
1053+
return ft.result();
10101054
}

‎test/testcondition.cpp‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class TestCondition : public TestFixture {
131131
TEST_CASE(knownConditionIncDecOperator);
132132
TEST_CASE(knownConditionFloating);
133133
TEST_CASE(knownConditionRecordedComparison);
134+
TEST_CASE(knownConditionAfterConditionalEscape); // #15264
134135
}
135136

136137
struct CheckOptions
@@ -6957,6 +6958,121 @@ class TestCondition : public TestFixture {
69576958
errout_str());
69586959
}
69596960

6961+
void knownConditionAfterConditionalEscape() {
6962+
// the then-block falls through when g() and b are true, so 'a' can be true after it
6963+
check("bool g();\n"
6964+
"void use();\n"
6965+
"void f(bool a, bool b) {\n"
6966+
" if (a) {\n"
6967+
" bool w = false;\n"
6968+
" if (g())\n"
6969+
" w = true;\n"
6970+
" if (!w || !b)\n"
6971+
" return;\n"
6972+
" }\n"
6973+
" if (b && a)\n"
6974+
" use();\n"
6975+
"}\n");
6976+
ASSERT_EQUALS("", errout_str());
6977+
6978+
// the then-path of 'if (g())' stops in its branch, but the then-block falls through when h() is true
6979+
check("bool g();\n"
6980+
"bool h();\n"
6981+
"void use();\n"
6982+
"void f(bool a, bool b) {\n"
6983+
" if (a) {\n"
6984+
" bool w = false;\n"
6985+
" if (g()) {\n"
6986+
" if (h())\n"
6987+
" w = true;\n"
6988+
" else\n"
6989+
" return;\n"
6990+
" }\n"
6991+
" if (!w || !b)\n"
6992+
" return;\n"
6993+
" }\n"
6994+
" if (b && a)\n"
6995+
" use();\n"
6996+
"}\n");
6997+
ASSERT_EQUALS("", errout_str());
6998+
6999+
// every path through the then-block returns
7000+
check("bool g();\n"
7001+
"void use();\n"
7002+
"void f(bool a, bool b) {\n"
7003+
" if (a) {\n"
7004+
" bool w = false;\n"
7005+
" if (g())\n"
7006+
" w = true;\n"
7007+
" if (!w || !b)\n"
7008+
" return;\n"
7009+
" return;\n"
7010+
" }\n"
7011+
" if (b && a)\n"
7012+
" use();\n"
7013+
"}\n");
7014+
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:12:14]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n", errout_str());
7015+
7016+
// a fork inside a loop body does not fall through the then-block
7017+
check("bool g();\n"
7018+
"void use();\n"
7019+
"void f(bool a, bool b, int n) {\n"
7020+
" if (a) {\n"
7021+
" bool z = false;\n"
7022+
" for (int i = 0; i < n; i++) {\n"
7023+
" if (g())\n"
7024+
" use();\n"
7025+
" }\n"
7026+
" if (!z)\n"
7027+
" return;\n"
7028+
" }\n"
7029+
" if (b && a)\n"
7030+
" use();\n"
7031+
"}\n");
7032+
ASSERT_EQUALS("[test.cpp:10:13]: (style) Condition '!z' is always true [knownConditionTrueFalse]\n"
7033+
"[test.cpp:4:9] -> [test.cpp:13:14]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n",
7034+
errout_str());
7035+
7036+
// the then-path is not forked when the fork limit is reached, but it falls through
7037+
const Settings settingsNormal = settingsBuilder().severity(Severity::style).checkLevel(Settings::CheckLevel::normal).build();
7038+
check("bool g();\n"
7039+
"bool h();\n"
7040+
"void use();\n"
7041+
"void f(bool a, bool b) {\n"
7042+
" if (a) {\n"
7043+
" bool w = false;\n"
7044+
" bool v = false;\n"
7045+
" if (g())\n"
7046+
" w = true;\n"
7047+
" if (h())\n"
7048+
" v = true;\n"
7049+
" if (!w || !v)\n"
7050+
" return;\n"
7051+
" }\n"
7052+
" if (b && a)\n"
7053+
" use();\n"
7054+
"}\n", settingsNormal);
7055+
ASSERT_EQUALS("", errout_str());
7056+
7057+
// the path that falls through the inner if returns too
7058+
check("bool g();\n"
7059+
"void use();\n"
7060+
"void f(bool a) {\n"
7061+
" if (a) {\n"
7062+
" bool w = false;\n"
7063+
" if (g())\n"
7064+
" w = true;\n"
7065+
" if (!w)\n"
7066+
" return;\n"
7067+
" a = false;\n"
7068+
" return;\n"
7069+
" }\n"
7070+
" if (a)\n"
7071+
" use();\n"
7072+
"}\n");
7073+
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:13:9]: (style) Condition 'a' is always false [knownConditionTrueFalse]\n", errout_str());
7074+
}
7075+
69607076
// TODO: a comparison of two expressions without values should decide the comparisons of the pair
69617077
// that move a constant to the other side: "(x + 1) < w[0]" is "x < w[0] - 1". At the normal check
69627078
// level the forward analysis forks on a few unknown conditions only, so the condition has to be

‎test/testnullpointer.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class TestNullPointer : public TestFixture {
174174
TEST_CASE(nullpointerDelete);
175175
TEST_CASE(nullpointerSubFunction);
176176
TEST_CASE(nullpointerExit);
177+
TEST_CASE(nullpointerAfterConditionalEscape); // #15264
177178
TEST_CASE(nullpointerStdString);
178179
TEST_CASE(nullpointerStdStream);
179180
TEST_CASE(nullpointerSmartPointer);
@@ -4270,6 +4271,25 @@ class TestNullPointer : public TestFixture {
42704271
ASSERT_EQUALS("", errout_str());
42714272
}
42724273

4274+
void nullpointerAfterConditionalEscape() {
4275+
// the then-block falls through when g() is true, so p can be null after it
4276+
check("bool g();\n"
4277+
"void use(int);\n"
4278+
"void f(int *p) {\n"
4279+
" if (!p) {\n"
4280+
" bool w = false;\n"
4281+
" if (g())\n"
4282+
" w = true;\n"
4283+
" if (!w)\n"
4284+
" return;\n"
4285+
" }\n"
4286+
" use(*p);\n"
4287+
"}\n");
4288+
ASSERT_EQUALS(
4289+
"[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",
4290+
errout_str());
4291+
}
4292+
42734293
void nullpointerStdString() {
42744294
check("void f(std::string s1) {\n"
42754295
" void* p = 0;\n"

‎test/testother.cpp‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,77 @@ class TestOther : public TestFixture {
936936
" return 0;\n"
937937
"}\n");
938938
ASSERT_EQUALS("[test.cpp:4:26]: (warning) Division by zero. [zerodivcond]\n", errout_str());
939+
940+
// the path that falls through the then-block assigns a (#15264)
941+
check("bool g();\n"
942+
"int f(int a) {\n"
943+
" if (a == 0) {\n"
944+
" bool w = false;\n"
945+
" if (g())\n"
946+
" w = true;\n"
947+
" if (!w)\n"
948+
" return 0;\n"
949+
" a = 1;\n"
950+
" }\n"
951+
" return 10 / a;\n"
952+
"}\n");
953+
ASSERT_EQUALS("", errout_str());
954+
955+
check("bool g();\n"
956+
"int f(int a, int n) {\n"
957+
" if (a == 0) {\n"
958+
" for (int i = 0; i < n; i++) {\n"
959+
" bool w = false;\n"
960+
" if (g())\n"
961+
" w = true;\n"
962+
" if (!w)\n"
963+
" return 0;\n"
964+
" }\n"
965+
" a = 1;\n"
966+
" }\n"
967+
" return 10 / a;\n"
968+
"}\n");
969+
ASSERT_EQUALS("", errout_str());
970+
971+
// the then-paths that are not followed at the normal check level return as well
972+
Settings settingsNormal = settingsBuilder(settings1).checkLevel(Settings::CheckLevel::normal).build();
973+
check("bool g();\n"
974+
"bool h();\n"
975+
"void use(int);\n"
976+
"int f(int x) {\n"
977+
" if (x == 0) {\n"
978+
" bool w = true;\n"
979+
" if (g())\n"
980+
" use(1);\n"
981+
" if (h())\n"
982+
" use(2);\n"
983+
" if (w) {\n"
984+
" use(3);\n"
985+
" return 0;\n"
986+
" }\n"
987+
" }\n"
988+
" return 10 / x;\n"
989+
"}\n", dinit(CheckOptions, $.settings = &settingsNormal));
990+
ASSERT_EQUALS("", errout_str());
991+
992+
// a fork that stops at the branch limit does not fall through with the assignment
993+
check("bool g(int);\n"
994+
"void use(int);\n"
995+
"int f(int x) {\n"
996+
" if (x == 0) {\n"
997+
" bool w = false;\n"
998+
" if (g(0))\n"
999+
" w = true;\n"
1000+
" if (!w)\n"
1001+
" return 0;\n"
1002+
" if (g(1)) use(1);\n"
1003+
" if (g(2)) use(2);\n"
1004+
" if (g(3)) use(3);\n"
1005+
" x = 1;\n"
1006+
" }\n"
1007+
" return 10 / x;\n"
1008+
"}\n", dinit(CheckOptions, $.settings = &settingsNormal));
1009+
ASSERT_EQUALS("", errout_str());
9391010
}
9401011

9411012
void zeroDivErrorPath() {

0 commit comments

Comments
 (0)