Fixed #623 (False positive: possible null pointer dereference when using the ?: operator)

This commit is contained in:
Daniel Marjamäki 2009-08-28 08:48:37 +02:00
parent dd64637ff1
commit 5d2c409e46
2 changed files with 26 additions and 1 deletions

View File

@ -1185,7 +1185,7 @@ void CheckOther::nullPointer()
{ {
if (tok1->varId() == varid) if (tok1->varId() == varid)
{ {
if (tok1->previous() && tok1->previous()->str() == "*" && tok1->tokAt(-2)->str() != "*") if (Token::Match(tok1->tokAt(-2), "[=;{}] *"))
{ {
nullPointerError(tok1, varname); nullPointerError(tok1, varname);
break; break;

View File

@ -504,6 +504,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str());
checkNullPointer("void foo(struct ABC *abc)\n"
"{\n"
" bar(abc->a);\n"
" if (!abc)\n"
" ;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str());
// ok dereferencing in a condition // ok dereferencing in a condition
checkNullPointer("void foo(struct ABC *abc)\n" checkNullPointer("void foo(struct ABC *abc)\n"
"{\n" "{\n"
@ -592,6 +600,14 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str());
checkNullPointer("void foo(int *p)\n"
"{\n"
" bar(*p);\n"
" if (!p)\n"
" ;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str());
// no error // no error
checkNullPointer("void foo()\n" checkNullPointer("void foo()\n"
"{\n" "{\n"
@ -628,6 +644,15 @@ private:
" ;\n" " ;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkNullPointer("void foo(int *p)\n"
"{\n"
" int var1 = p ? *p : 0;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }