Skip to content

Commit a31ee17

Browse files
committed
Also accept string literals and expressions that cannot be 0 in isObjectAddress
1 parent 9790581 commit a31ee17

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎lib/programmemory.cpp‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,13 +1703,26 @@ namespace {
17031703
return !values.empty() && values.front().isImpossible();
17041704
}
17051705

1706-
// Is the value of the pointer the address of an object, so that it is not null?
1706+
// Is the value of the pointer the address of an object, so that it is not null? It is when the
1707+
// pointer was made with &, or when its value refers to a string literal or to an expression
1708+
// that cannot be 0, such as &x or an array
17071709
static bool isObjectAddress(const Token* expr, const Values& values)
17081710
{
1709-
if (!expr->valueType() || expr->valueType()->pointer == 0)
1711+
if (!expr->valueType() || expr->valueType()->pointer == 0 || values.size() != 1)
17101712
return false;
1711-
return values.size() == 1 && values.front().isLifetimeValue() &&
1712-
values.front().lifetimeKind == ValueFlow::Value::LifetimeKind::Address;
1713+
const ValueFlow::Value& value = values.front();
1714+
if (value.isLifetimeValue() && value.lifetimeKind == ValueFlow::Value::LifetimeKind::Address)
1715+
return true;
1716+
if (!value.isLifetimeValue() && !value.isTokValue() && !(value.isSymbolicValue() && value.intvalue == 0))
1717+
return false;
1718+
const Token* tok = value.tokvalue;
1719+
if (!tok)
1720+
return false;
1721+
if (tok->tokType() == Token::eString)
1722+
return true;
1723+
return std::any_of(tok->values().cbegin(), tok->values().cend(), [](const ValueFlow::Value& v) {
1724+
return v.isImpossible() && v.isIntValue() && v.intvalue == 0;
1725+
});
17131726
}
17141727

17151728
// The value of a condition: constraints that exclude zero are true, a value stands for itself

‎test/testnullpointer.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,6 +3389,27 @@ class TestNullPointer : public TestFixture {
33893389
"}\n");
33903390
ASSERT_EQUALS("", errout_str());
33913391

3392+
check("void f(const char *s1, const char *s2) {\n"
3393+
" if (!s1 && !s2)\n"
3394+
" return;\n"
3395+
" char c = s1 ? *s1 : *s2;\n"
3396+
" (void)c;\n"
3397+
"}\n"
3398+
"void g1() {\n"
3399+
" char c = 0;\n"
3400+
" char *p = &c;\n"
3401+
" f(p, nullptr);\n"
3402+
"}\n"
3403+
"void g2() {\n"
3404+
" char a[2] = {0};\n"
3405+
" char *p = a;\n"
3406+
" f(p, nullptr);\n"
3407+
"}\n"
3408+
"void g3() {\n"
3409+
" f(\"x\", nullptr);\n"
3410+
"}\n");
3411+
ASSERT_EQUALS("", errout_str());
3412+
33923413
check("void f(double *d1, double *d2) {\n"
33933414
" if (!d1 && !d2)\n"
33943415
" return;\n"

0 commit comments

Comments
 (0)