Skip to content
Open
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
28 changes: 28 additions & 0 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,28 @@ namespace {
return !values.empty() && values.front().isImpossible();
}

// 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 || values.size() != 1)
return false;
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
static ValueFlow::Value conditionValue(Values values)
{
Expand Down Expand Up @@ -2134,6 +2156,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)) {
Expand Down
48 changes: 48 additions & 0 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3375,6 +3376,53 @@ 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(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"
" 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"
Expand Down