From 034140e7e1c679584aa7399cd6141b1c88ae8d98 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 27 Apr 2022 19:52:56 +0200 Subject: [PATCH] Fix #10991 FN: Redundant pointer operation (#4054) --- lib/checkother.cpp | 19 ++++++++++++------- lib/checkother.h | 4 ++-- test/testother.cpp | 10 ++++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 487a18e56..16c74f5e2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2941,7 +2941,12 @@ void CheckOther::checkRedundantPointerOp() if (tok->isExpandedMacro() && tok->str() == "(") tok = tok->link(); - if (!tok->isUnaryOp("&") || !tok->astOperand1()->isUnaryOp("*")) + bool addressOfDeref{}; + if (tok->isUnaryOp("&") && tok->astOperand1()->isUnaryOp("*")) + addressOfDeref = true; + else if (tok->isUnaryOp("*") && tok->astOperand1()->isUnaryOp("&")) + addressOfDeref = false; + else continue; // variable @@ -2950,18 +2955,18 @@ void CheckOther::checkRedundantPointerOp() continue; const Variable *var = varTok->variable(); - if (!var || !var->isPointer()) + if (!var || (addressOfDeref && !var->isPointer())) continue; - redundantPointerOpError(tok, var->name(), false); + redundantPointerOpError(tok, var->name(), false, addressOfDeref); } } -void CheckOther::redundantPointerOpError(const Token* tok, const std::string &varname, bool inconclusive) +void CheckOther::redundantPointerOpError(const Token* tok, const std::string &varname, bool inconclusive, bool addressOfDeref) { - reportError(tok, Severity::style, "redundantPointerOp", - "$symbol:" + varname + "\n" - "Redundant pointer operation on '$symbol' - it's already a pointer.", CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); + std::string msg = "$symbol:" + varname + "\nRedundant pointer operation on '$symbol' - it's already a "; + msg += addressOfDeref ? "pointer." : "variable."; + reportError(tok, Severity::style, "redundantPointerOp", msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); } void CheckOther::checkInterlockedDecrement() diff --git a/lib/checkother.h b/lib/checkother.h index 86da1db08..8f8403526 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -275,7 +275,7 @@ private: void incompleteArrayFillError(const Token* tok, const std::string& buffer, const std::string& function, bool boolean); void varFuncNullUBError(const Token *tok); void commaSeparatedReturnError(const Token *tok); - void redundantPointerOpError(const Token* tok, const std::string& varname, bool inconclusive); + void redundantPointerOpError(const Token* tok, const std::string& varname, bool inconclusive, bool addressOfDeref); void raceAfterInterlockedDecrementError(const Token* tok); void unusedLabelError(const Token* tok, bool inSwitch, bool hasIfdef); void unknownEvaluationOrder(const Token* tok); @@ -343,7 +343,7 @@ private: c.varFuncNullUBError(nullptr); c.nanInArithmeticExpressionError(nullptr); c.commaSeparatedReturnError(nullptr); - c.redundantPointerOpError(nullptr, "varname", false); + c.redundantPointerOpError(nullptr, "varname", false, /*addressOfDeref*/ true); c.unusedLabelError(nullptr, false, false); c.unusedLabelError(nullptr, false, true); c.unusedLabelError(nullptr, true, false); diff --git a/test/testother.cpp b/test/testother.cpp index 23c987333..06e13e0b2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -8764,6 +8764,16 @@ private: "}\n", nullptr, false, true); ASSERT_EQUALS("[test.cpp:2]: (style) Redundant pointer operation on 'y' - it's already a pointer.\n", errout.str()); + check("int f() {\n" // #10991 + " int value = 4;\n" + " int result1 = *(&value);\n" + " int result2 = *&value;\n" + " return result1 + result2;\n" + "}\n", nullptr, false, true); + ASSERT_EQUALS("[test.cpp:3]: (style) Redundant pointer operation on 'value' - it's already a variable.\n" + "[test.cpp:4]: (style) Redundant pointer operation on 'value' - it's already a variable.\n", + errout.str()); + // no warning for bitwise AND check("void f(const int *b) {\n" " int x = 0x20 & *b;\n"