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
11 changes: 8 additions & 3 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ static ProgramMemory getInitialProgramState(const Token* tok,
ProgramMemoryState::ProgramMemoryState(const Settings& s) : settings(s), changedCache(std::make_shared<ChangedCache>())
{}

void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin, bool skipUnknown)
{
if (origin)
for (const auto& p : pm)
origins[p.first.getExpressionId()] = origin;
state.replace(std::move(pm), /*skipUnknown*/ true);
state.replace(std::move(pm), skipUnknown);
}

static void addVars(ProgramMemory& pm, const ProgramMemory::Map& vars)
Expand All @@ -723,6 +723,8 @@ void ProgramMemoryState::addState(const Token* tok, const ProgramMemory::Map& va

void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty, const Token* origin)
{
// An origin is only given when the true path is assumed again at the end of its block
const bool reassumed = origin != nullptr;
ProgramMemory pm = state;
if (isEmpty)
pm.setContainerSizeValue(tok, 0, b);
Expand All @@ -741,7 +743,10 @@ void ProgramMemoryState::assume(const Token* tok, bool b, bool isEmpty, const To
}
}
}
replace(std::move(pm), origin);
// pm is the state with the condition applied, so the condition also replaces an unknown value. Not when
// it is assumed again at the end of the block: the path past the if would then know values that the
// forward analysis does not lower when only other paths modify the tracked value.
replace(std::move(pm), origin, /*skipUnknown*/ reassumed);
}

ProgramMemoryState::FindChangedFn ProgramMemoryState::getCachedFindExpressionChanged(bool skipDeadCode) const
Expand Down
2 changes: 1 addition & 1 deletion lib/programmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ struct CPPCHECKLIB ProgramMemoryState {

explicit ProgramMemoryState(const Settings& s);

void replace(ProgramMemory pm, const Token* origin = nullptr);
void replace(ProgramMemory pm, const Token* origin = nullptr, bool skipUnknown = true);

void addState(const Token* tok, const ProgramMemory::Map& vars);

Expand Down
26 changes: 26 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class TestCondition : public TestFixture {
TEST_CASE(knownConditionCast); // #9976
TEST_CASE(knownConditionIncrementLoop); // #9808
TEST_CASE(knownConditionAfterBailout); // #12526
TEST_CASE(knownConditionAfterIfNull); // #15265
TEST_CASE(knownConditionIncDecOperator);
TEST_CASE(knownConditionFloating);
TEST_CASE(knownConditionRecordedComparison);
Expand Down Expand Up @@ -6768,6 +6769,31 @@ class TestCondition : public TestFixture {
ASSERT_EQUALS("[test.cpp:13:11] -> [test.cpp:18:13]: (style) Condition 'mS.b' is always false [knownConditionTrueFalse]\n", errout_str());
}

void knownConditionAfterIfNull() {
// flag is set on the path where p is not null
check("bool g();\n"
"bool h();\n"
"int* get();\n"
"void use();\n"
"void f(bool c) {\n"
" bool flag = false;\n"
" int* p = get();\n"
" if (p == nullptr) {\n"
" if (g())\n"
" return;\n"
" }\n"
" if (c) {\n"
" if (h()) {\n"
" if (p != nullptr && *p > 0)\n"
" flag = true;\n"
" }\n"
" }\n"
" if (flag)\n"
" use();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void knownConditionIncDecOperator() {
check(
"void f() {\n"
Expand Down
28 changes: 28 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6893,6 +6893,34 @@ class TestUninitVar : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:12]: (error) Uninitialized variable: a [uninitvar]\n"
"[test.cpp:8:11]: (error) Uninitialized variable: a [uninitvar]\n",
errout_str());

// #15265 - 'err' is unknown before the condition, but not 0 on the path where sq is not set
valueFlowUninit("int calcA();\n"
"int calcB(double *r);\n"
"void print(double d);\n"
"void f() {\n"
" double sq;\n"
" int err = calcA();\n"
" if (err == 0)\n"
" err = calcB(&sq);\n"
" print(err == 0 ? sq : 0.0);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

valueFlowUninit("bool g();\n"
"int calcA();\n"
"int calcB(double *r);\n"
"void print(double d);\n"
"void f() {\n"
" bool const a = g();\n"
" double sq;\n"
" int err = calcA();\n"
" if (err == 0)\n"
" err = calcB(&sq);\n"
" if (!a)\n"
" print(err == 0 ? sq : 0.0);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
Expand Down
Loading