Fix ctunullpointer FP (#4471)

This commit is contained in:
chrchr-github 2022-09-16 12:11:34 +02:00 committed by GitHub
parent 888721ea12
commit 45ccc9ba1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -146,6 +146,12 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) const
return isPointerDeRef(tok, unknown, mSettings); return isPointerDeRef(tok, unknown, mSettings);
} }
static bool isUnevaluated(const Token* tok) {
if (tok && Token::Match(tok->previous(), "sizeof|decltype ("))
return true;
return false;
}
bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings) bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings)
{ {
unknown = false; unknown = false;
@ -182,7 +188,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set
return false; return false;
// Dereferencing pointer.. // Dereferencing pointer..
if (parent->isUnaryOp("*")) { if (parent->isUnaryOp("*") && !isUnevaluated(parent->astParent())) {
// declaration of function pointer // declaration of function pointer
if (tok->variable() && tok->variable()->nameToken() == tok) if (tok->variable() && tok->variable()->nameToken() == tok)
return false; return false;

View File

@ -4341,6 +4341,15 @@ private:
" g(x);\n" " g(x);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
ctu("size_t f(int* p) {\n"
" size_t len = sizeof(*p);\n"
" return len;\n"
"}\n"
"void g() {\n"
" f(NULL);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
}; };