Null pointer: Added check for 'if (fred); return fred->a;'
This commit is contained in:
parent
6e14da7eba
commit
a6740200cf
|
@ -678,8 +678,12 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
|||
|
||||
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))
|
||||
{
|
||||
bool dummy = false;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -1148,6 +1148,12 @@ private:
|
|||
" len = decltype(*pFoo);\n"
|
||||
"}");
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue