Fixed #2159 (False positive: Null pointer dereference 'if (!x) return *y;')

This commit is contained in:
Daniel Marjamäki 2010-11-01 19:21:08 +01:00
parent 3fb17baa12
commit 94fc13f0c4
2 changed files with 10 additions and 3 deletions

View File

@ -461,7 +461,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (Token::Match(tok2, "goto|return|continue|break|throw|if")) if (Token::Match(tok2, "goto|return|continue|break|throw|if"))
{ {
if (Token::Match(tok2, "return * %var%")) if (Token::Match(tok2, "return * %varid%", varid))
nullPointerError(tok2, tok->strAt(3)); nullPointerError(tok2, tok->strAt(3));
break; break;
} }

View File

@ -43,7 +43,7 @@ private:
TEST_CASE(nullpointer7); TEST_CASE(nullpointer7);
TEST_CASE(nullpointer8); TEST_CASE(nullpointer8);
TEST_CASE(nullpointer9); TEST_CASE(nullpointer9);
TEST_CASE(nullpointer10); // check if pointer is null and then dereference it TEST_CASE(checkAndDeRef); // check if pointer is null and then dereference it
} }
void check(const char code[]) void check(const char code[])
@ -631,7 +631,7 @@ private:
} }
// Check if pointer is null and the dereference it // Check if pointer is null and the dereference it
void nullpointer10() void checkAndDeRef()
{ {
check("void foo(char *p) {\n" check("void foo(char *p) {\n"
" if (!p) {\n" " if (!p) {\n"
@ -671,6 +671,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void foo(bool p) {\n"
" if (!p) {\n"
" }\n"
" return *x;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// This is why this check can't be used on the simplified token list // This is why this check can't be used on the simplified token list
check("void f(Foo *foo) {\n" check("void f(Foo *foo) {\n"
" if (!dynamic_cast<bar *>(foo)) {\n" " if (!dynamic_cast<bar *>(foo)) {\n"