Skip to content

Commit 6cd3e47

Browse files
committed
Fix FN
1 parent fb32f4b commit 6cd3e47

4 files changed

Lines changed: 115 additions & 2 deletions

File tree

‎lib/forwardanalyzer.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,14 +655,21 @@ namespace {
655655
std::accumulate(branches.cbegin(), branches.cend(), Analyzer::Action{}, [](Analyzer::Action a, const Branch& branch) {
656656
return a | branch.action;
657657
});
658-
actions |= action;
658+
// With a split the block of a fork is not on this path
659+
for (const Branch& branch : branches) {
660+
if (split && branch.traversal != this)
661+
parted.action |= branch.action;
662+
else
663+
actions |= branch.action;
664+
}
659665
const bool jumps = std::any_of(branches.cbegin(), branches.cend(), std::mem_fn(&Branch::hasGoto));
660666
const bool reachesEnd = std::all_of(branches.cbegin(), branches.cend(), [this](const Branch& branch) {
661667
return branch.traversal != this || branch.reachesEnd;
662668
});
663669
Progress progress = reachesEnd ? Progress::Continue : Progress::Break;
664670
if (reachesEnd) {
665-
if (std::any_of(branches.cbegin(), branches.cend(), std::mem_fn(&Branch::isDead)) && conditional && stopUpdates())
671+
if (std::any_of(branches.cbegin(), branches.cend(), std::mem_fn(&Branch::isDead)) && conditional &&
672+
(stopUpdates() || action.isModified()))
666673
progress = Break(Analyzer::Terminate::Conditional);
667674
else if (!lowerAfterBranches(*analyzer, branches) || jumps)
668675
progress = Break(Analyzer::Terminate::Bail);

‎test/testnullpointer.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,33 @@ class TestNullPointer : public TestFixture {
34403440
" use(*p);\n"
34413441
"}\n");
34423442
ASSERT_EQUALS("", errout_str());
3443+
3444+
check("bool g();\n" // w is set in a loop
3445+
"void use(int);\n"
3446+
"void f(const int *p, const int *q, int n) {\n"
3447+
" if (!p) {\n"
3448+
" bool w = false;\n"
3449+
" for (int i = 0; i < n; i++) {\n"
3450+
" if (g())\n"
3451+
" w = true;\n"
3452+
" }\n"
3453+
" if (w)\n"
3454+
" p = q;\n"
3455+
" }\n"
3456+
" use(*p);\n"
3457+
"}\n");
3458+
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:13:10]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n",
3459+
errout_str());
3460+
3461+
check("void use(int);\n" // p is only possibly null in the block, and w may depend on it
3462+
"void f(const int *p, const int *q, bool b, bool w) {\n"
3463+
" if (!p || b) {\n"
3464+
" if (w)\n"
3465+
" p = q;\n"
3466+
" }\n"
3467+
" use(*p);\n"
3468+
"}\n");
3469+
ASSERT_EQUALS("", errout_str());
34433470
}
34443471

34453472
void nullpointer_addressOf() { // address of

‎test/testother.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,59 @@ class TestOther : public TestFixture {
10371037
" return 10 / a;\n"
10381038
"}\n");
10391039
ASSERT_EQUALS("", errout_str());
1040+
1041+
check("bool g();\n" // w is set in a loop
1042+
"int f(int a, int n) {\n"
1043+
" if (a == 0) {\n"
1044+
" bool w = false;\n"
1045+
" for (int i = 0; i < n; i++) {\n"
1046+
" if (g())\n"
1047+
" w = true;\n"
1048+
" }\n"
1049+
" if (w)\n"
1050+
" a = 1;\n"
1051+
" }\n"
1052+
" return 10 / a;\n"
1053+
"}\n");
1054+
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:12:15]: (warning) Either the condition 'a==0' is redundant or there is division by zero at line 12. [zerodivcond]\n",
1055+
errout_str());
1056+
1057+
check("bool g();\n" // w is set in a do-while loop
1058+
"int f(int a, int n) {\n"
1059+
" if (a == 0) {\n"
1060+
" bool w = false;\n"
1061+
" do {\n"
1062+
" if (g())\n"
1063+
" w = true;\n"
1064+
" } while (n--);\n"
1065+
" if (w)\n"
1066+
" a = 1;\n"
1067+
" }\n"
1068+
" return 10 / a;\n"
1069+
"}\n");
1070+
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:12:15]: (warning) Either the condition 'a==0' is redundant or there is division by zero at line 12. [zerodivcond]\n",
1071+
errout_str());
1072+
1073+
check("int f(int a, bool w) {\n" // a is kept when w is false
1074+
" if (a == 0) {\n"
1075+
" if (w)\n"
1076+
" a = 1;\n"
1077+
" }\n"
1078+
" return 10 / a;\n"
1079+
"}\n");
1080+
ASSERT_EQUALS("[test.cpp:2:11] -> [test.cpp:6:15]: (warning) Either the condition 'a==0' is redundant or there is division by zero at line 6. [zerodivcond]\n",
1081+
errout_str());
1082+
1083+
check("int f(int a, bool w) {\n" // both blocks change a
1084+
" if (a == 0) {\n"
1085+
" if (w)\n"
1086+
" a = 1;\n"
1087+
" else\n"
1088+
" a = 2;\n"
1089+
" }\n"
1090+
" return 10 / a;\n"
1091+
"}\n");
1092+
ASSERT_EQUALS("", errout_str());
10401093
}
10411094

10421095
void zeroDivErrorPath() {

‎test/testvalueflow.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4144,6 +4144,32 @@ class TestValueFlow : public TestFixture {
41444144
"}\n";
41454145
ASSERT_EQUALS(false, testValueOfX(code, 9U, 0));
41464146

4147+
// w is set in a loop, where the paths do not part: the condition on w is undecided
4148+
code = "bool g();\n"
4149+
"int f(int x, int n) {\n"
4150+
" if (x == 0) {\n"
4151+
" bool w = false;\n"
4152+
" for (int i = 0; i < n; i++) { if (g()) w = true; }\n"
4153+
" if (w) x = 1;\n"
4154+
" }\n"
4155+
" return x;\n"
4156+
"}\n";
4157+
ASSERT_EQUALS(true, testValueOfX(code, 8U, 0));
4158+
ASSERT_EQUALS(false, testValueOfXKnown(code, 8U, 0));
4159+
4160+
// .. and the value is gone when the path that keeps x returns
4161+
code = "bool g();\n"
4162+
"int f(int x, int n) {\n"
4163+
" if (x == 0) {\n"
4164+
" bool w = false;\n"
4165+
" for (int i = 0; i < n; i++) { if (g()) w = true; }\n"
4166+
" if (w) x = 1;\n"
4167+
" else return 0;\n"
4168+
" }\n"
4169+
" return x;\n"
4170+
"}\n";
4171+
ASSERT_EQUALS(false, testValueOfX(code, 9U, 0));
4172+
41474173
// one path through the loop body stops at the switch: x is still known after the loop,
41484174
// which is only left when the condition is false
41494175
code = "bool g();\n"

0 commit comments

Comments
 (0)