Fixed #5082 (False positive: (error) Possible null pointer dereference: p2)

This commit is contained in:
Daniel Marjamäki 2013-10-20 13:37:36 +02:00
parent 47766736fc
commit fc26de89a9
2 changed files with 14 additions and 4 deletions

View File

@ -1401,12 +1401,12 @@ private:
bool null;
/** variable is set to null */
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid) {
static void setnull(std::list<ExecutionPath *> &checks, const unsigned int varid, bool null) {
std::list<ExecutionPath *>::iterator it;
for (it = checks.begin(); it != checks.end(); ++it) {
Nullpointer *c = dynamic_cast<Nullpointer *>(*it);
if (c && c->varId == varid)
c->null = true;
c->null = null;
}
}
@ -1483,8 +1483,8 @@ private:
dereference(checks, &tok);
else if (unknown && owner->inconclusiveFlag())
dereference(checks, &tok);
if (Token::Match(tok.previous(), "[;{}=] %var% = 0 ;"))
setnull(checks, tok.varId());
if (Token::Match(tok.previous(), "[;{}=] %var% ="))
setnull(checks, tok.varId(), Token::simpleMatch(tok.tokAt(2), "0 ;"));
else if (!deref &&
(!tok.previous()->isOp() || tok.previous()->str() == "&") &&
(!tok.next()->isConstOp() || tok.next()->str() == ">>"))

View File

@ -55,6 +55,7 @@ private:
TEST_CASE(nullpointer20); // #3807 (fp: return p ? (p->x() || p->y()) : z)
TEST_CASE(nullpointer21); // #4038 (fp: if (x) p=q; else return;)
TEST_CASE(nullpointer23); // #4665 (false positive)
TEST_CASE(nullpointer24); // #5082 fp: chained assignment
TEST_CASE(nullpointer_cast); // #4692
TEST_CASE(nullpointer_castToVoid); // #3771
TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it
@ -1241,6 +1242,15 @@ private:
TODO_ASSERT_EQUALS("","[test.cpp:4]: (error) Possible null pointer dereference: c\n", errout.str());
}
void nullpointer24() { // #5083 - fp: chained assignment
check("void f(){\n"
" char *c = NULL;\n"
" x = c = new char[10];\n"
" *c = 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void nullpointer_cast() { // #4692
check("char *nasm_skip_spaces(const char *p) {\n"
" if (p)\n"