From 9790581c538b2735f153a288fcfad0ed3c5de7b7 Mon Sep 17 00:00:00 2001 From: APX403 Date: Thu, 19 Nov 2026 08:58:29 +0200 Subject: [PATCH 1/2] Fix #14747 FP nullPointer for ternary with previous null pointer check When the arguments of a call such as f(&d, NULL) are followed together, the tracked value of the first pointer is the address of d. The program memory had no truth value for an address, so after "if (!d1 && !d2) return;" the condition of "d1 ? *d1 : *d2" stayed unknown and the null value of d2 reached *d2. A pointer whose tracked value is the address of an object is now true when used as a bool. --- lib/programmemory.cpp | 15 +++++++++++++++ test/testnullpointer.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 95d977e5f26..aa4bfed3ebd 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1703,6 +1703,15 @@ namespace { return !values.empty() && values.front().isImpossible(); } + // Is the value of the pointer the address of an object, so that it is not null? + static bool isObjectAddress(const Token* expr, const Values& values) + { + if (!expr->valueType() || expr->valueType()->pointer == 0) + return false; + return values.size() == 1 && values.front().isLifetimeValue() && + values.front().lifetimeKind == ValueFlow::Value::LifetimeKind::Address; + } + // The value of a condition: constraints that exclude zero are true, a value stands for itself static ValueFlow::Value conditionValue(Values values) { @@ -2134,6 +2143,12 @@ namespace { const Values* stored = pm->getValues(expr->exprId()); if (!stored || *stored != *tracked) pm->setValues(expr, *tracked); + // A pointer to an object is true as a bool + if (isObjectAddress(expr, *tracked) && isUsedAsBool(expr, settings)) { + ValueFlow::Value result{1}; + result.setKnown(); + return single(std::move(result)); + } return *tracked; } if (const Values* stored = getStoredValues(expr)) { diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index cecbb27ca09..a7b7fb3c06d 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -154,6 +154,7 @@ class TestNullPointer : public TestFixture { TEST_CASE(nullpointer113); // a variable is clamped before the check TEST_CASE(nullpointer114); // the else block changes the value TEST_CASE(nullpointer115); // a range condition overlapping the assumed one + TEST_CASE(nullpointer116); // #14747 TEST_CASE(nullpointer_addressOf); // address of TEST_CASE(nullpointerSwitch); // #2626 TEST_CASE(nullpointer_cast); // #4692 @@ -3375,6 +3376,32 @@ class TestNullPointer : public TestFixture { ASSERT_EQUALS("[test.cpp:4:24]: (warning) Possible null pointer dereference: p [nullPointer]\n", errout_str()); } + void nullpointer116() { // #14747 + check("void f(double *d1, double *d2) {\n" + " if (!d1 && !d2)\n" + " return;\n" + " double d3 = d1 ? *d1 : *d2;\n" + " (void)d3;\n" + "}\n" + "void g() {\n" + " double d = 0;\n" + " f(&d, nullptr);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("void f(double *d1, double *d2) {\n" + " if (!d1 && !d2)\n" + " return;\n" + " double d3 = d1 ? *d2 : 0;\n" + " (void)d3;\n" + "}\n" + "void g() {\n" + " double d = 0;\n" + " f(&d, nullptr);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:23]: (warning) Possible null pointer dereference: d2 [nullPointer]\n", errout_str()); + } + void nullpointer_addressOf() { // address of check("void f() {\n" " struct X *x = 0;\n" From a31ee176785534feb3393b01fb528b04c0252d99 Mon Sep 17 00:00:00 2001 From: APX403 Date: Fri, 27 Nov 2026 17:27:47 +0200 Subject: [PATCH 2/2] Also accept string literals and expressions that cannot be 0 in isObjectAddress --- lib/programmemory.cpp | 21 +++++++++++++++++---- test/testnullpointer.cpp | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index aa4bfed3ebd..d89a10d53cf 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1703,13 +1703,26 @@ namespace { return !values.empty() && values.front().isImpossible(); } - // Is the value of the pointer the address of an object, so that it is not null? + // Is the value of the pointer the address of an object, so that it is not null? It is when the + // pointer was made with &, or when its value refers to a string literal or to an expression + // that cannot be 0, such as &x or an array static bool isObjectAddress(const Token* expr, const Values& values) { - if (!expr->valueType() || expr->valueType()->pointer == 0) + if (!expr->valueType() || expr->valueType()->pointer == 0 || values.size() != 1) return false; - return values.size() == 1 && values.front().isLifetimeValue() && - values.front().lifetimeKind == ValueFlow::Value::LifetimeKind::Address; + const ValueFlow::Value& value = values.front(); + if (value.isLifetimeValue() && value.lifetimeKind == ValueFlow::Value::LifetimeKind::Address) + return true; + if (!value.isLifetimeValue() && !value.isTokValue() && !(value.isSymbolicValue() && value.intvalue == 0)) + return false; + const Token* tok = value.tokvalue; + if (!tok) + return false; + if (tok->tokType() == Token::eString) + return true; + return std::any_of(tok->values().cbegin(), tok->values().cend(), [](const ValueFlow::Value& v) { + return v.isImpossible() && v.isIntValue() && v.intvalue == 0; + }); } // The value of a condition: constraints that exclude zero are true, a value stands for itself diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index a7b7fb3c06d..f8585c2a8f2 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3389,6 +3389,27 @@ class TestNullPointer : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); + check("void f(const char *s1, const char *s2) {\n" + " if (!s1 && !s2)\n" + " return;\n" + " char c = s1 ? *s1 : *s2;\n" + " (void)c;\n" + "}\n" + "void g1() {\n" + " char c = 0;\n" + " char *p = &c;\n" + " f(p, nullptr);\n" + "}\n" + "void g2() {\n" + " char a[2] = {0};\n" + " char *p = a;\n" + " f(p, nullptr);\n" + "}\n" + "void g3() {\n" + " f(\"x\", nullptr);\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + check("void f(double *d1, double *d2) {\n" " if (!d1 && !d2)\n" " return;\n"