Fix #11007 FP nullPointerRedundantCheck with static function pointer (#4051)

This commit is contained in:
chrchr-github 2022-04-26 17:25:56 +02:00 committed by GitHub
parent 0d35a60954
commit 0dc3cb6eba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -182,8 +182,13 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set
return false; return false;
// Dereferencing pointer.. // Dereferencing pointer..
if (parent->isUnaryOp("*") && !addressOf) if (parent->isUnaryOp("*")) {
return true; // declaration of function pointer
if (tok->variable() && tok->variable()->nameToken() == tok)
return false;
if (!addressOf)
return true;
}
// array access // array access
if (firstOperand && parent->str() == "[" && !addressOf) if (firstOperand && parent->str() == "[" && !addressOf)

View File

@ -1137,6 +1137,15 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: f\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: f\n", errout.str());
check("int* g();\n" // #11007
"int* f() {\n"
" static int* (*fun)() = 0;\n"
" if (!fun)\n"
" fun = g;\n"
" return fun();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// loops.. // loops..
check("void f() {\n" check("void f() {\n"
" int *p = 0;\n" " int *p = 0;\n"