Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/analyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ struct Analyzer {
virtual void assume(const Token* tok, bool state, unsigned int flags = 0) = 0;
/// Update the state of the program at the token
virtual void updateState(const Token* tok) = 0;
/// Go back to the program state of 'before' (an earlier copy of this analyzer) and update it at the token
virtual void resetState(const Analyzer& /*before*/, const Token* tok) {
updateState(tok);
}
/// Return analyzer for expression at token
virtual ValuePtr<Analyzer> reanalyze(Token* tok, const std::string& msg = "") const = 0;
virtual bool invalid() const {
Expand Down
30 changes: 27 additions & 3 deletions lib/forwardanalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ namespace {
int forkDepth = 0;
// Total forks of the traversal (shared via the fork() copy); backstop past the depth bound.
std::shared_ptr<int> forkBudget = std::make_shared<int>(0);
// End of the whole traversal; a fork inside a branch or loop body only walks to the block end
const Token* traversalEnd = nullptr;
// A then-path carried past an if in the current block reached the block end and ends there
bool forkLostAtBlockEnd = false;

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

ForwardTraversal fork(bool analyze = false) const {
ForwardTraversal ft = *this;
ft.forkLostAtBlockEnd = false;
if (analyze) {
ft.analyzeOnly = true;
ft.analyzeTerminate = true;
Expand Down Expand Up @@ -402,12 +407,27 @@ namespace {
return a;
}

// A then-path carried past an if inside the block ends at the block end. This traversal assumed
// that path's condition false, so after the block it has to stand for that path as well: what it
// learned in the block is dropped again.
Progress updateBlock(Token* endBlock, int depth)
{
const ValuePtr<Analyzer> before = analyzer;
const bool outerForkLost = forkLostAtBlockEnd;
forkLostAtBlockEnd = false;
const Progress p = updateRange(endBlock->link(), endBlock, depth);
if (forkLostAtBlockEnd && p != Progress::Break)
analyzer->resetState(*before, endBlock);
forkLostAtBlockEnd = outerForkLost;
return p;
}

Progress updateBranch(Branch& branch, int depth)
{
// Save and reset actions
Analyzer::Action prevActions = actions;
actions = Analyzer::Action::None;
Progress p = updateRange(branch.endBlock->link(), branch.endBlock, depth);
Progress p = updateBlock(branch.endBlock, depth);
branch.action |= actions;
// Restore actions
actions |= prevActions;
Expand Down Expand Up @@ -624,7 +644,7 @@ namespace {
if (!endBlock)
return Break();
updateScopeState(endBlock);
return updateRange(endBlock->link(), endBlock, depth);
return updateBlock(endBlock, depth);
}

/**
Expand Down Expand Up @@ -837,7 +857,10 @@ namespace {
++(*forkBudget);
++ft.forkDepth;
ft.analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
ft.updateRange(tok->next(), end, depth - 1);
const Progress pCarried = ft.updateRange(tok->next(), end, depth - 1);
// Inside a branch or loop body the fork walks only to the block end
if (pCarried != Progress::Break && end != traversalEnd)
forkLostAtBlockEnd = true;
}
if (elseBranch.progress == Progress::Break) {
// Only the else branch escaped; the then branch falls through, so
Expand Down Expand Up @@ -992,6 +1015,7 @@ Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const V
if (a->invalid())
return Analyzer::Result{Analyzer::Action::None, Analyzer::Terminate::Bail};
ForwardTraversal ft{a, tokenList, errorLogger, settings};
ft.traversalEnd = end;
if (start)
ft.analyzer->updateState(start);
ft.updateRange(start, end);
Expand Down
8 changes: 8 additions & 0 deletions lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,14 @@ struct ValueFlowAnalyzer : Analyzer {
pms.addState(tok, getProgramState());
}

void resetState(const Analyzer& before, const Token* tok) override
{
// 'before' is a copy of this analyzer
assert(dynamic_cast<const ValueFlowAnalyzer*>(&before));
pms = static_cast<const ValueFlowAnalyzer&>(before).pms;
updateState(tok);
}

void update(Token* tok, Action a, Direction d) override {
ValueFlow::Value* value = getValue(tok);
if (!value)
Expand Down
22 changes: 22 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class TestCondition : public TestFixture {
TEST_CASE(knownConditionIncDecOperator);
TEST_CASE(knownConditionFloating);
TEST_CASE(knownConditionRecordedComparison);
TEST_CASE(knownConditionFlagSetInNestedBranch);
}

struct CheckOptions
Expand Down Expand Up @@ -6957,6 +6958,27 @@ class TestCondition : public TestFixture {
errout_str());
}

void knownConditionFlagSetInNestedBranch() {
check("bool g();\n" // #15266
"bool h();\n"
"int send();\n"
"int f() {\n"
" int x = 0;\n"
" bool b = false;\n"
" if (g()) {\n"
" if (h())\n"
" b = true;\n"
" }\n"
" if (b) {\n"
" if (x == 0)\n"
" x = send();\n"
" }\n"
" return x;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12:15]: (style) Condition 'x==0' is always true [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
Expand Down
26 changes: 26 additions & 0 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,32 @@ class TestNullPointer : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

// inside a block that does not return
check("void g(int);\n"
"void f(int x, int c) {\n"
" int *p = 0;\n"
" if (c) {\n"
" if (x > 5) x = 5;\n"
" if (x > 5) g(*p);\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

// the flag is set in a nested branch
check("bool g();\n" // #15266
"int f(int c) {\n"
" int *p = 0;\n"
" bool b = false;\n"
" if (c) {\n"
" if (g())\n"
" b = true;\n"
" }\n"
" if (b)\n"
" return *p;\n"
" return 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10:17]: (error) Null pointer dereference: p [nullPointer]\n", errout_str());

// false on both paths
check("int f(int x) {\n"
" int *p = 0;\n"
Expand Down
13 changes: 13 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,19 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

// inside a block that does not return
check("void g(int);\n"
"int f(int x) {\n"
" int d = 0;\n"
" if (x >= 0) {\n"
" if (x > 5) x = 0;\n"
" if (x <= 5) d = 1;\n"
" g(10 / d);\n"
" }\n"
" return d;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("int f(int x) {\n"
" int d = 0;\n"
" if (x >= 0) {\n"
Expand Down
14 changes: 14 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7976,6 +7976,20 @@ class TestUninitVar : public TestFixture {
" *v[0] = 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("struct D { int a; int b; };\n" // #15266
"void use(D d);\n"
"void f(const int* p, int n) {\n"
" D d;\n"
" d.a = 0;\n"
" bool found = false;\n"
" for (int i = 0; i < n; i++)\n"
" if (p[i] == 0)\n"
" found = true;\n"
" if (found)\n"
" use(d);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10:9] -> [test.cpp:11:13]: (warning) Uninitialized variable: d.b [uninitvar]\n", errout_str());
}

void valueFlowUninitForLoop()
Expand Down
26 changes: 26 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4674,6 +4674,32 @@ class TestValueFlow : public TestFixture {
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 5U, 0));

// the fork that sets the flag in a nested branch ends with the outer block, so the main path
// must not drop it: the value still reaches the branch on the flag
code = "bool g();\n"
"bool h();\n"
"void f() {\n"
" int x = 1;\n"
" bool b = false;\n"
" if (g()) {\n"
" if (h())\n"
" b = true;\n"
" }\n"
" if (b) { int a = x; }\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 10U, 1));

// the same with the flag set in a loop
code = "void f(const int* p, int n) {\n"
" int x = 1;\n"
" bool b = false;\n"
" for (int i = 0; i < n; i++)\n"
" if (p[i] == 0)\n"
" b = true;\n"
" if (b) { int a = x; }\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXKnown(code, 7U, 1));

// no fork: the main path still knows the condition is false
/*const*/ Settings s(settings);
s.vfOptions.maxForwardConditionForkDepth = 0;
Expand Down
Loading