Null pointer: Added check for 'if (fred); return fred->a;'

This commit is contained in:
Daniel Marjamäki 2011-07-25 21:58:00 +02:00
parent 6e14da7eba
commit a6740200cf
2 changed files with 11 additions and 1 deletions

View File

@ -678,8 +678,12 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch")) if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))
{ {
bool dummy = false;
if (Token::Match(tok2, "return * %varid%", varid)) if (Token::Match(tok2, "return * %varid%", varid))
nullPointerError(tok2, tok->strAt(3), linenr); nullPointerError(tok2, pointerName, linenr);
else if (Token::Match(tok2, "return %varid%", varid) &&
CheckNullPointer::isPointerDeRef(tok2->next(), dummy))
nullPointerError(tok2, pointerName, linenr);
break; break;
} }

View File

@ -1148,6 +1148,12 @@ private:
" len = decltype(*pFoo);\n" " len = decltype(*pFoo);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("int foo(struct Fred *fred) {\n"
" if (fred) { int a = 0; }\n"
" return fred->a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str());
} }
// Test CheckNullPointer::nullConstantDereference // Test CheckNullPointer::nullConstantDereference