From 45ccc9ba1ee7eeaf3657e42087feece0588adf38 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 16 Sep 2022 12:11:34 +0200 Subject: [PATCH] Fix ctunullpointer FP (#4471) --- lib/checknullpointer.cpp | 8 +++++++- test/testnullpointer.cpp | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 4fbe58785..b895c6bbd 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -146,6 +146,12 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) const 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) { unknown = false; @@ -182,7 +188,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set return false; // Dereferencing pointer.. - if (parent->isUnaryOp("*")) { + if (parent->isUnaryOp("*") && !isUnevaluated(parent->astParent())) { // declaration of function pointer if (tok->variable() && tok->variable()->nameToken() == tok) return false; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index e0475c95b..e86cebd83 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -4341,6 +4341,15 @@ private: " g(x);\n" "}"); 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()); } };