Skip to content

Commit 03c04ee

Browse files
committed
Handle the false positive
1 parent d639598 commit 03c04ee

6 files changed

Lines changed: 205 additions & 8 deletions

File tree

‎lib/analyzer.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,16 @@ struct Analyzer {
132132
unsigned int mFlag{};
133133
};
134134

135-
enum class Terminate : std::uint8_t { None, Bail, Escape, Modified, Inconclusive, Conditional };
135+
enum class Terminate : std::uint8_t {
136+
None,
137+
Bail,
138+
Escape,
139+
Modified,
140+
Inconclusive,
141+
Conditional,
142+
// Not all paths terminate
143+
Partial
144+
};
136145

137146
struct Result {
138147
explicit Result(Action action = Action::None, Terminate terminate = Terminate::None)
@@ -142,7 +151,9 @@ struct Analyzer {
142151
Terminate terminate;
143152

144153
void update(Result rhs) {
145-
if (terminate == Terminate::None)
154+
// A value that stopped on every path decides over one that reached the end on some
155+
const bool reached = terminate == Terminate::None || terminate == Terminate::Partial;
156+
if (reached && rhs.terminate != Terminate::None)
146157
terminate = rhs.terminate;
147158
action |= rhs.action;
148159
}

‎lib/forwardanalyzer.cpp‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ namespace {
119119
return 3;
120120
case Analyzer::Terminate::Bail:
121121
return 4;
122+
case Analyzer::Terminate::Partial:
123+
break;
122124
}
123125
cppcheck::unreachable();
124126
}
@@ -890,7 +892,23 @@ namespace {
890892
// As updatePaths(), and this traversal gets the joint result of the paths. It must not go on
891893
// after the range.
892894
void updateAllPaths(Token* start, const Token* end, int depth = 20) {
893-
const Outcome paths = updatePaths(start, end, depth);
895+
setJointResult(updatePaths(start, end, depth), end);
896+
}
897+
898+
// As updateAllPaths(), for a caller that may go on with the value after the range. The joint
899+
// result alone would drop a value that this path keeps to the end while a path that parted
900+
// from it for good changes it or stops (e.g. 'if (g()) w = 1; if (w) x = 1;' when g()
901+
// returns false).
902+
Analyzer::Result updateRangeResult(Token* start, const Token* end) {
903+
const Progress p = updateRange(start, end, true);
904+
const bool unchanged = p == Progress::Continue && !actions.isModified();
905+
setJointResult(outcome(p), end);
906+
if (unchanged && (actions.isModified() || terminate != Analyzer::Terminate::None))
907+
terminate = Analyzer::Terminate::Partial;
908+
return Analyzer::Result{actions, terminate};
909+
}
910+
911+
void setJointResult(const Outcome& paths, const Token* end) {
894912
// A path that stopped without an escape was not followed to one, but the code can show
895913
// that there is no way to the end of the range
896914
const bool escapes = paths.allEscape() ||
@@ -1224,8 +1242,7 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V
12241242
ForwardTraversal ft{a, tokenList, errorLogger, settings};
12251243
if (start)
12261244
ft.analyzer->updateState(start);
1227-
ft.updateAllPaths(start, end);
1228-
return Analyzer::Result{ ft.actions, ft.terminate };
1245+
return ft.updateRangeResult(start, end);
12291246
}
12301247

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

‎lib/valueflow.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4868,6 +4868,8 @@ struct ConditionHandler {
48684868
}
48694869

48704870
bool deadBranch[] = {false, false};
4871+
// The value is changed on one path through the block and kept on another
4872+
bool partialBranch[] = {false, false};
48714873
// start token of conditional code
48724874
Token* startTokens[] = {nullptr, nullptr};
48734875
// determine startToken(s)
@@ -4889,12 +4891,15 @@ struct ConditionHandler {
48894891

48904892
Analyzer::Result r = forward(startTokens[i], startTokens[i]->link(), cond.vartok, values, tokenlist, errorLogger, settings);
48914893
deadBranch[i] = r.terminate == Analyzer::Terminate::Escape;
4892-
if (r.action.isModified() && !deadBranch[i])
4894+
partialBranch[i] = r.terminate == Analyzer::Terminate::Partial;
4895+
if (r.action.isModified() && !deadBranch[i] && !partialBranch[i])
48934896
changeBlock = i;
48944897
if (r.terminate != Analyzer::Terminate::None && r.terminate != Analyzer::Terminate::Escape &&
4895-
r.terminate != Analyzer::Terminate::Modified)
4898+
r.terminate != Analyzer::Terminate::Modified && !partialBranch[i])
48964899
bailBlock = i;
48974900
changeKnownToPossible(values);
4901+
if (partialBranch[i])
4902+
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
48984903
}
48994904
if (changeBlock >= 0 && !Token::simpleMatch(top->previous(), "while (")) {
49004905
if (settings.debugwarnings)
@@ -4990,7 +4995,7 @@ struct ConditionHandler {
49904995
if (possible) {
49914996
values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible));
49924997
changeKnownToPossible(values);
4993-
} else if (allowKnown) {
4998+
} else if (allowKnown && !partialBranch[dead_if ? 1 : 0]) {
49944999
valueFlowSetConditionToKnown(condTok, values, true);
49955000
valueFlowSetConditionToKnown(condTok, values, false);
49965001
}

‎test/testnullpointer.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,37 @@ class TestNullPointer : public TestFixture {
34093409
"}\n");
34103410
ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:14:10]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n",
34113411
errout_str());
3412+
3413+
check("bool g();\n" // the path that sets w assigns p, the other path keeps it
3414+
"void use(int);\n"
3415+
"void f(const int *p, const int *q) {\n"
3416+
" if (!p) {\n"
3417+
" bool w = false;\n"
3418+
" if (g())\n"
3419+
" w = true;\n"
3420+
" if (w)\n"
3421+
" p = q;\n"
3422+
" }\n"
3423+
" use(*p);\n"
3424+
"}\n");
3425+
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",
3426+
errout_str());
3427+
3428+
check("bool g();\n" // the path that keeps p returns
3429+
"void use(int);\n"
3430+
"void f(const int *p, const int *q) {\n"
3431+
" if (!p) {\n"
3432+
" bool w = false;\n"
3433+
" if (g())\n"
3434+
" w = true;\n"
3435+
" if (w)\n"
3436+
" p = q;\n"
3437+
" else\n"
3438+
" return;\n"
3439+
" }\n"
3440+
" use(*p);\n"
3441+
"}\n");
3442+
ASSERT_EQUALS("", errout_str());
34123443
}
34133444

34143445
void nullpointer_addressOf() { // address of

‎test/testother.cpp‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,70 @@ class TestOther : public TestFixture {
973973
TODO_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",
974974
"",
975975
errout_str());
976+
977+
// the path that sets w changes a, the other path keeps it
978+
check("bool g();\n"
979+
"int f(int a) {\n"
980+
" if (a == 0) {\n"
981+
" bool w = false;\n"
982+
" if (g())\n"
983+
" w = true;\n"
984+
" if (w)\n"
985+
" a = 1;\n"
986+
" }\n"
987+
" return 10 / a;\n"
988+
"}\n");
989+
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:10:15]: (warning) Either the condition 'a==0' is redundant or there is division by zero at line 10. [zerodivcond]\n",
990+
errout_str());
991+
992+
check("bool g();\n" // the path that keeps a goes through an else block
993+
"bool h();\n"
994+
"int f(int a) {\n"
995+
" if (a == 0) {\n"
996+
" bool w = false;\n"
997+
" if (g())\n"
998+
" w = true;\n"
999+
" if (w) {\n"
1000+
" a = 1;\n"
1001+
" } else {\n"
1002+
" if (h())\n"
1003+
" return 0;\n"
1004+
" }\n"
1005+
" }\n"
1006+
" return 10 / a;\n"
1007+
"}\n");
1008+
ASSERT_EQUALS("[test.cpp:4:11] -> [test.cpp:15:15]: (warning) Either the condition 'a==0' is redundant or there is division by zero at line 15. [zerodivcond]\n",
1009+
errout_str());
1010+
1011+
check("bool g();\n" // the path that keeps a returns
1012+
"int f(int a) {\n"
1013+
" if (a == 0) {\n"
1014+
" bool w = false;\n"
1015+
" if (g())\n"
1016+
" w = true;\n"
1017+
" if (w)\n"
1018+
" a = 1;\n"
1019+
" if (!w)\n"
1020+
" return 0;\n"
1021+
" }\n"
1022+
" return 10 / a;\n"
1023+
"}\n");
1024+
ASSERT_EQUALS("", errout_str());
1025+
1026+
check("bool g();\n" // every path changes a
1027+
"int f(int a) {\n"
1028+
" if (a == 0) {\n"
1029+
" bool w = false;\n"
1030+
" if (g())\n"
1031+
" w = true;\n"
1032+
" if (w)\n"
1033+
" a = 1;\n"
1034+
" if (!w)\n"
1035+
" a = 2;\n"
1036+
" }\n"
1037+
" return 10 / a;\n"
1038+
"}\n");
1039+
ASSERT_EQUALS("", errout_str());
9761040
}
9771041

9781042
void zeroDivErrorPath() {

‎test/testvalueflow.cpp‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,6 +4088,75 @@ class TestValueFlow : public TestFixture {
40884088
"}\n";
40894089
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
40904090
ASSERT_EQUALS(true, testValueOfXImpossible(code, 4U, 0));
4091+
4092+
// the path that sets w changes x, the other path keeps it
4093+
code = "bool g();\n"
4094+
"int f(int x) {\n"
4095+
" if (x == 0) {\n"
4096+
" bool w = false;\n"
4097+
" if (g()) w = true;\n"
4098+
" if (w) x = 1;\n"
4099+
" }\n"
4100+
" return x;\n"
4101+
"}\n";
4102+
ASSERT_EQUALS(true, testValueOfX(code, 8U, 0));
4103+
ASSERT_EQUALS(false, testValueOfXKnown(code, 8U, 0));
4104+
4105+
// .. x is not known afterwards when the else block returns
4106+
code = "bool g();\n"
4107+
"int f(int x) {\n"
4108+
" if (x == 0) {\n"
4109+
" bool w = false;\n"
4110+
" if (g()) w = true;\n"
4111+
" if (w) x = 1;\n"
4112+
" } else {\n"
4113+
" return 0;\n"
4114+
" }\n"
4115+
" return x;\n"
4116+
"}\n";
4117+
ASSERT_EQUALS(true, testValueOfX(code, 10U, 0));
4118+
ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 0));
4119+
4120+
// the path that sets w stops at the switch, the other path keeps x
4121+
code = "bool g();\n"
4122+
"void use(int);\n"
4123+
"int f(int x, int n) {\n"
4124+
" if (x == 0) {\n"
4125+
" bool w = false;\n"
4126+
" if (g()) w = true;\n"
4127+
" if (w) { switch (n) { case 1: use(n); break; } }\n"
4128+
" }\n"
4129+
" return x;\n"
4130+
"}\n";
4131+
ASSERT_EQUALS(true, testValueOfX(code, 9U, 0));
4132+
ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 0));
4133+
4134+
// the path that keeps x returns
4135+
code = "bool g();\n"
4136+
"int f(int x) {\n"
4137+
" if (x == 0) {\n"
4138+
" bool w = false;\n"
4139+
" if (g()) w = true;\n"
4140+
" if (w) x = 1;\n"
4141+
" if (!w) return 0;\n"
4142+
" }\n"
4143+
" return x;\n"
4144+
"}\n";
4145+
ASSERT_EQUALS(false, testValueOfX(code, 9U, 0));
4146+
4147+
// one path through the loop body stops at the switch: x is still known after the loop,
4148+
// which is only left when the condition is false
4149+
code = "bool g();\n"
4150+
"void use(int);\n"
4151+
"int f(int x, int n) {\n"
4152+
" while (x != 0) {\n"
4153+
" bool w = false;\n"
4154+
" if (g()) w = true;\n"
4155+
" if (w) { switch (n) { case 1: use(n); } }\n"
4156+
" }\n"
4157+
" return x;\n"
4158+
"}\n";
4159+
ASSERT_EQUALS(true, testValueOfXKnown(code, 9U, 0));
40914160
}
40924161

40934162
void valueFlowAfterConditionTernary()

0 commit comments

Comments
 (0)