Skip to content

Commit fc26de8

Browse files
committed
Fixed cppcheck-opensource#5082 (False positive: (error) Possible null pointer dereference: p2)
1 parent 4776673 commit fc26de8

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/checknullpointer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,12 +1401,12 @@ class Nullpointer : public ExecutionPath {
14011401
bool null;
14021402

14031403
/** variable is set to null */
1404-
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid) {
1404+
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid, bool null) {
14051405
std::list<ExecutionPath *>::iterator it;
14061406
for (it = checks.begin(); it != checks.end(); ++it) {
14071407
Nullpointer *c = dynamic_cast<Nullpointer *>(*it);
14081408
if (c && c->varId == varid)
1409-
c->null = true;
1409+
c->null = null;
14101410
}
14111411
}
14121412

@@ -1483,8 +1483,8 @@ class Nullpointer : public ExecutionPath {
14831483
dereference(checks, &tok);
14841484
else if (unknown && owner->inconclusiveFlag())
14851485
dereference(checks, &tok);
1486-
if (Token::Match(tok.previous(), "[;{}=] %var% = 0 ;"))
1487-
setnull(checks, tok.varId());
1486+
if (Token::Match(tok.previous(), "[;{}=] %var% ="))
1487+
setnull(checks, tok.varId(), Token::simpleMatch(tok.tokAt(2), "0 ;"));
14881488
else if (!deref &&
14891489
(!tok.previous()->isOp() || tok.previous()->str() == "&") &&
14901490
(!tok.next()->isConstOp() || tok.next()->str() == ">>"))

test/testnullpointer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestNullPointer : public TestFixture {
5555
TEST_CASE(nullpointer20); // #3807 (fp: return p ? (p->x() || p->y()) : z)
5656
TEST_CASE(nullpointer21); // #4038 (fp: if (x) p=q; else return;)
5757
TEST_CASE(nullpointer23); // #4665 (false positive)
58+
TEST_CASE(nullpointer24); // #5082 fp: chained assignment
5859
TEST_CASE(nullpointer_cast); // #4692
5960
TEST_CASE(nullpointer_castToVoid); // #3771
6061
TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it
@@ -1241,6 +1242,15 @@ class TestNullPointer : public TestFixture {
12411242
TODO_ASSERT_EQUALS("","[test.cpp:4]: (error) Possible null pointer dereference: c\n", errout.str());
12421243
}
12431244

1245+
void nullpointer24() { // #5083 - fp: chained assignment
1246+
check("void f(){\n"
1247+
" char *c = NULL;\n"
1248+
" x = c = new char[10];\n"
1249+
" *c = 0;\n"
1250+
"}");
1251+
ASSERT_EQUALS("", errout.str());
1252+
}
1253+
12441254
void nullpointer_cast() { // #4692
12451255
check("char *nasm_skip_spaces(const char *p) {\n"
12461256
" if (p)\n"

0 commit comments

Comments
 (0)