diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 95d977e5f26..91a1abf248a 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -695,12 +695,12 @@ static ProgramMemory getInitialProgramState(const Token* tok, ProgramMemoryState::ProgramMemoryState(const Settings& s) : settings(s), changedCache(std::make_shared()) {} -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) @@ -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); @@ -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 diff --git a/lib/programmemory.h b/lib/programmemory.h index a201a09f6fc..375c6a2aca1 100644 --- a/lib/programmemory.h +++ b/lib/programmemory.h @@ -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); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 5961dca21a9..5af410dfd62 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -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); @@ -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" diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 787ede556ea..49ea9fe6a02 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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