Skip to content

Commit 79522f6

Browse files
autoantwortclaude
andcommitted
Fix #15265 FP uninitvar: ProgramMemoryState::assume() drops a condition on a variable assigned an unknown value
A variable assigned an unknown value is stored in the program memory as unknown. Since 35dc425 (#13930) ProgramMemoryState::replace() keeps an unknown value (skipUnknown), and assume() stores the state with the condition applied through it, so the condition is dropped: on the false path of 'if (err == 0)' err is not known to be non-zero. 4b66976 (#15042) made this visible in more cases. The true path that is assumed again at the end of its block keeps an unknown value as before: with the condition, the path past the if knows values that the forward analysis does not lower when only other paths modify the tracked value (knownConditionTrueFalse FP). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 61d2460 commit 79522f6

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎lib/programmemory.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,12 @@ static ProgramMemory getInitialProgramState(const Token* tok,
695695
ProgramMemoryState::ProgramMemoryState(const Settings& s) : settings(s), changedCache(std::make_shared<ChangedCache>())
696696
{}
697697

698-
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin)
698+
void ProgramMemoryState::replace(ProgramMemory pm, const Token* origin, bool skipUnknown)
699699
{
700700
if (origin)
701701
for (const auto& p : pm)
702702
origins[p.first.getExpressionId()] = origin;
703-
state.replace(std::move(pm), /*skipUnknown*/ true);
703+
state.replace(std::move(pm), skipUnknown);
704704
}
705705

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

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

747752
ProgramMemoryState::FindChangedFn ProgramMemoryState::getCachedFindExpressionChanged(bool skipDeadCode) const

‎lib/programmemory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ struct CPPCHECKLIB ProgramMemoryState {
215215

216216
explicit ProgramMemoryState(const Settings& s);
217217

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

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

‎test/testcondition.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class TestCondition : public TestFixture {
128128
TEST_CASE(knownConditionCast); // #9976
129129
TEST_CASE(knownConditionIncrementLoop); // #9808
130130
TEST_CASE(knownConditionAfterBailout); // #12526
131+
TEST_CASE(knownConditionAfterIfNull); // #15265
131132
TEST_CASE(knownConditionIncDecOperator);
132133
TEST_CASE(knownConditionFloating);
133134
TEST_CASE(knownConditionRecordedComparison);
@@ -6768,6 +6769,31 @@ class TestCondition : public TestFixture {
67686769
ASSERT_EQUALS("[test.cpp:13:11] -> [test.cpp:18:13]: (style) Condition 'mS.b' is always false [knownConditionTrueFalse]\n", errout_str());
67696770
}
67706771

6772+
void knownConditionAfterIfNull() {
6773+
// flag is set on the path where p is not null
6774+
check("bool g();\n"
6775+
"bool h();\n"
6776+
"int* get();\n"
6777+
"void use();\n"
6778+
"void f(bool c) {\n"
6779+
" bool flag = false;\n"
6780+
" int* p = get();\n"
6781+
" if (p == nullptr) {\n"
6782+
" if (g())\n"
6783+
" return;\n"
6784+
" }\n"
6785+
" if (c) {\n"
6786+
" if (h()) {\n"
6787+
" if (p != nullptr && *p > 0)\n"
6788+
" flag = true;\n"
6789+
" }\n"
6790+
" }\n"
6791+
" if (flag)\n"
6792+
" use();\n"
6793+
"}\n");
6794+
ASSERT_EQUALS("", errout_str());
6795+
}
6796+
67716797
void knownConditionIncDecOperator() {
67726798
check(
67736799
"void f() {\n"

‎test/testuninitvar.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6893,6 +6893,34 @@ class TestUninitVar : public TestFixture {
68936893
ASSERT_EQUALS("[test.cpp:3:12]: (error) Uninitialized variable: a [uninitvar]\n"
68946894
"[test.cpp:8:11]: (error) Uninitialized variable: a [uninitvar]\n",
68956895
errout_str());
6896+
6897+
// #15265 - 'err' is unknown before the condition, but not 0 on the path where sq is not set
6898+
valueFlowUninit("int calcA();\n"
6899+
"int calcB(double *r);\n"
6900+
"void print(double d);\n"
6901+
"void f() {\n"
6902+
" double sq;\n"
6903+
" int err = calcA();\n"
6904+
" if (err == 0)\n"
6905+
" err = calcB(&sq);\n"
6906+
" print(err == 0 ? sq : 0.0);\n"
6907+
"}\n");
6908+
ASSERT_EQUALS("", errout_str());
6909+
6910+
valueFlowUninit("bool g();\n"
6911+
"int calcA();\n"
6912+
"int calcB(double *r);\n"
6913+
"void print(double d);\n"
6914+
"void f() {\n"
6915+
" bool const a = g();\n"
6916+
" double sq;\n"
6917+
" int err = calcA();\n"
6918+
" if (err == 0)\n"
6919+
" err = calcB(&sq);\n"
6920+
" if (!a)\n"
6921+
" print(err == 0 ? sq : 0.0);\n"
6922+
"}\n");
6923+
ASSERT_EQUALS("", errout_str());
68966924
}
68976925

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

0 commit comments

Comments
 (0)