Skip to content

Commit 50bc17b

Browse files
ls-tetysclaude
authored andcommitted
Fix #15266 FN knownConditionTrueFalse/uninitvar: ValueFlow drops the path where a flag is set in a nested branch or loop (regression of #15042)
The then-path of an if is carried past it on a fork, but only to the end of the current range. Inside a branch or loop body it ends at the block end, while the traversal assumed its condition false, so e.g. a branch on a flag set in a nested if or loop got no values (knownConditionTrueFalse, uninitvar and nullPointer false negatives). Now when that happens the traversal drops the program state it learned in the block at the block end, so it stands for the lost path afterwards; inside the block it stays precise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 61d2460 commit 50bc17b

8 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎lib/analyzer.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ struct Analyzer {
189189
virtual void assume(const Token* tok, bool state, unsigned int flags = 0) = 0;
190190
/// Update the state of the program at the token
191191
virtual void updateState(const Token* tok) = 0;
192+
/// Go back to the program state of 'before' (an earlier copy of this analyzer) and update it at the token
193+
virtual void resetState(const Analyzer& /*before*/, const Token* tok) {
194+
updateState(tok);
195+
}
192196
/// Return analyzer for expression at token
193197
virtual ValuePtr<Analyzer> reanalyze(Token* tok, const std::string& msg = "") const = 0;
194198
virtual bool invalid() const {

‎lib/forwardanalyzer.cpp‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ 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+
// End of the whole traversal; a fork inside a branch or loop body only walks to the block end
68+
const Token* traversalEnd = nullptr;
69+
// A then-path carried past an if in the current block reached the block end and ends there
70+
bool forkLostAtBlockEnd = false;
6771

6872
Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) {
6973
if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None)
@@ -329,6 +333,7 @@ namespace {
329333

330334
ForwardTraversal fork(bool analyze = false) const {
331335
ForwardTraversal ft = *this;
336+
ft.forkLostAtBlockEnd = false;
332337
if (analyze) {
333338
ft.analyzeOnly = true;
334339
ft.analyzeTerminate = true;
@@ -402,12 +407,27 @@ namespace {
402407
return a;
403408
}
404409

410+
// A then-path carried past an if inside the block ends at the block end. This traversal assumed
411+
// that path's condition false, so after the block it has to stand for that path as well: what it
412+
// learned in the block is dropped again.
413+
Progress updateBlock(Token* endBlock, int depth)
414+
{
415+
const ValuePtr<Analyzer> before = analyzer;
416+
const bool outerForkLost = forkLostAtBlockEnd;
417+
forkLostAtBlockEnd = false;
418+
const Progress p = updateRange(endBlock->link(), endBlock, depth);
419+
if (forkLostAtBlockEnd && p != Progress::Break)
420+
analyzer->resetState(*before, endBlock);
421+
forkLostAtBlockEnd = outerForkLost;
422+
return p;
423+
}
424+
405425
Progress updateBranch(Branch& branch, int depth)
406426
{
407427
// Save and reset actions
408428
Analyzer::Action prevActions = actions;
409429
actions = Analyzer::Action::None;
410-
Progress p = updateRange(branch.endBlock->link(), branch.endBlock, depth);
430+
Progress p = updateBlock(branch.endBlock, depth);
411431
branch.action |= actions;
412432
// Restore actions
413433
actions |= prevActions;
@@ -624,7 +644,7 @@ namespace {
624644
if (!endBlock)
625645
return Break();
626646
updateScopeState(endBlock);
627-
return updateRange(endBlock->link(), endBlock, depth);
647+
return updateBlock(endBlock, depth);
628648
}
629649

630650
/**
@@ -837,7 +857,10 @@ namespace {
837857
++(*forkBudget);
838858
++ft.forkDepth;
839859
ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
840-
ft.updateRange(tok->next(), end, depth - 1);
860+
const Progress pCarried = ft.updateRange(tok->next(), end, depth - 1);
861+
// Inside a branch or loop body the fork walks only to the block end
862+
if (pCarried != Progress::Break && end != traversalEnd)
863+
forkLostAtBlockEnd = true;
841864
}
842865
if (elseBranch.progress == Progress::Break) {
843866
// 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
9921015
if (a->invalid())
9931016
return Analyzer::Result{Analyzer::Action::None, Analyzer::Terminate::Bail};
9941017
ForwardTraversal ft{a, tokenList, errorLogger, settings};
1018+
ft.traversalEnd = end;
9951019
if (start)
9961020
ft.analyzer->updateState(start);
9971021
ft.updateRange(start, end);

‎lib/vf_analyzers.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,14 @@ struct ValueFlowAnalyzer : Analyzer {
815815
pms.addState(tok, getProgramState());
816816
}
817817

818+
void resetState(const Analyzer& before, const Token* tok) override
819+
{
820+
// 'before' is a copy of this analyzer
821+
assert(dynamic_cast<const ValueFlowAnalyzer*>(&before));
822+
pms = static_cast<const ValueFlowAnalyzer&>(before).pms;
823+
updateState(tok);
824+
}
825+
818826
void update(Token* tok, Action a, Direction d) override {
819827
ValueFlow::Value* value = getValue(tok);
820828
if (!value)

‎test/testcondition.cpp‎

Lines changed: 22 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(knownConditionFlagSetInNestedBranch);
134135
}
135136

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

6961+
void knownConditionFlagSetInNestedBranch() {
6962+
check("bool g();\n" // #15266
6963+
"bool h();\n"
6964+
"int send();\n"
6965+
"int f() {\n"
6966+
" int x = 0;\n"
6967+
" bool b = false;\n"
6968+
" if (g()) {\n"
6969+
" if (h())\n"
6970+
" b = true;\n"
6971+
" }\n"
6972+
" if (b) {\n"
6973+
" if (x == 0)\n"
6974+
" x = send();\n"
6975+
" }\n"
6976+
" return x;\n"
6977+
"}\n");
6978+
ASSERT_EQUALS("[test.cpp:12:15]: (style) Condition 'x==0' is always true [knownConditionTrueFalse]\n",
6979+
errout_str());
6980+
}
6981+
69606982
// TODO: a comparison of two expressions without values should decide the comparisons of the pair
69616983
// that move a constant to the other side: "(x + 1) < w[0]" is "x < w[0] - 1". At the normal check
69626984
// level the forward analysis forks on a few unknown conditions only, so the condition has to be

‎test/testnullpointer.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,6 +3296,32 @@ class TestNullPointer : public TestFixture {
32963296
"}\n");
32973297
ASSERT_EQUALS("", errout_str());
32983298

3299+
// inside a block that does not return
3300+
check("void g(int);\n"
3301+
"void f(int x, int c) {\n"
3302+
" int *p = 0;\n"
3303+
" if (c) {\n"
3304+
" if (x > 5) x = 5;\n"
3305+
" if (x > 5) g(*p);\n"
3306+
" }\n"
3307+
"}\n");
3308+
ASSERT_EQUALS("", errout_str());
3309+
3310+
// the flag is set in a nested branch
3311+
check("bool g();\n" // #15266
3312+
"int f(int c) {\n"
3313+
" int *p = 0;\n"
3314+
" bool b = false;\n"
3315+
" if (c) {\n"
3316+
" if (g())\n"
3317+
" b = true;\n"
3318+
" }\n"
3319+
" if (b)\n"
3320+
" return *p;\n"
3321+
" return 0;\n"
3322+
"}\n");
3323+
ASSERT_EQUALS("[test.cpp:10:17]: (error) Null pointer dereference: p [nullPointer]\n", errout_str());
3324+
32993325
// false on both paths
33003326
check("int f(int x) {\n"
33013327
" int *p = 0;\n"

‎test/testother.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,19 @@ class TestOther : public TestFixture {
909909
"}\n");
910910
ASSERT_EQUALS("", errout_str());
911911

912+
// inside a block that does not return
913+
check("void g(int);\n"
914+
"int f(int x) {\n"
915+
" int d = 0;\n"
916+
" if (x >= 0) {\n"
917+
" if (x > 5) x = 0;\n"
918+
" if (x <= 5) d = 1;\n"
919+
" g(10 / d);\n"
920+
" }\n"
921+
" return d;\n"
922+
"}\n");
923+
ASSERT_EQUALS("", errout_str());
924+
912925
check("int f(int x) {\n"
913926
" int d = 0;\n"
914927
" if (x >= 0) {\n"

‎test/testuninitvar.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7976,6 +7976,20 @@ class TestUninitVar : public TestFixture {
79767976
" *v[0] = 0;\n"
79777977
"}\n");
79787978
ASSERT_EQUALS("", errout_str());
7979+
7980+
valueFlowUninit("struct D { int a; int b; };\n" // #15266
7981+
"void use(D d);\n"
7982+
"void f(const int* p, int n) {\n"
7983+
" D d;\n"
7984+
" d.a = 0;\n"
7985+
" bool found = false;\n"
7986+
" for (int i = 0; i < n; i++)\n"
7987+
" if (p[i] == 0)\n"
7988+
" found = true;\n"
7989+
" if (found)\n"
7990+
" use(d);\n"
7991+
"}\n");
7992+
ASSERT_EQUALS("[test.cpp:10:9] -> [test.cpp:11:13]: (warning) Uninitialized variable: d.b [uninitvar]\n", errout_str());
79797993
}
79807994

79817995
void valueFlowUninitForLoop()

‎test/testvalueflow.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4674,6 +4674,32 @@ class TestValueFlow : public TestFixture {
46744674
"}\n";
46754675
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));
46764676

4677+
// the fork that sets the flag in a nested branch ends with the outer block, so the main path
4678+
// must not drop it: the value still reaches the branch on the flag
4679+
code = "bool g();\n"
4680+
"bool h();\n"
4681+
"void f() {\n"
4682+
" int x = 1;\n"
4683+
" bool b = false;\n"
4684+
" if (g()) {\n"
4685+
" if (h())\n"
4686+
" b = true;\n"
4687+
" }\n"
4688+
" if (b) { int a = x; }\n"
4689+
"}\n";
4690+
ASSERT_EQUALS(true, testValueOfXKnown(code, 10U, 1));
4691+
4692+
// the same with the flag set in a loop
4693+
code = "void f(const int* p, int n) {\n"
4694+
" int x = 1;\n"
4695+
" bool b = false;\n"
4696+
" for (int i = 0; i < n; i++)\n"
4697+
" if (p[i] == 0)\n"
4698+
" b = true;\n"
4699+
" if (b) { int a = x; }\n"
4700+
"}\n";
4701+
ASSERT_EQUALS(true, testValueOfXKnown(code, 7U, 1));
4702+
46774703
// no fork: the main path still knows the condition is false
46784704
/*const*/ Settings s(settings);
46794705
s.vfOptions.maxForwardConditionForkDepth = 0;

0 commit comments

Comments
 (0)