Fix #10991 FN: Redundant pointer operation (#4054)

This commit is contained in:
chrchr-github 2022-04-27 19:52:56 +02:00 committed by GitHub
parent 3e3abecba0
commit 034140e7e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 9 deletions

View File

@ -2941,7 +2941,12 @@ void CheckOther::checkRedundantPointerOp()
if (tok->isExpandedMacro() && tok->str() == "(") if (tok->isExpandedMacro() && tok->str() == "(")
tok = tok->link(); 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; continue;
// variable // variable
@ -2950,18 +2955,18 @@ void CheckOther::checkRedundantPointerOp()
continue; continue;
const Variable *var = varTok->variable(); const Variable *var = varTok->variable();
if (!var || !var->isPointer()) if (!var || (addressOfDeref && !var->isPointer()))
continue; 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", std::string msg = "$symbol:" + varname + "\nRedundant pointer operation on '$symbol' - it's already a ";
"$symbol:" + varname + "\n" msg += addressOfDeref ? "pointer." : "variable.";
"Redundant pointer operation on '$symbol' - it's already a pointer.", CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal); reportError(tok, Severity::style, "redundantPointerOp", msg, CWE398, inconclusive ? Certainty::inconclusive : Certainty::normal);
} }
void CheckOther::checkInterlockedDecrement() void CheckOther::checkInterlockedDecrement()

View File

@ -275,7 +275,7 @@ private:
void incompleteArrayFillError(const Token* tok, const std::string& buffer, const std::string& function, bool boolean); void incompleteArrayFillError(const Token* tok, const std::string& buffer, const std::string& function, bool boolean);
void varFuncNullUBError(const Token *tok); void varFuncNullUBError(const Token *tok);
void commaSeparatedReturnError(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 raceAfterInterlockedDecrementError(const Token* tok);
void unusedLabelError(const Token* tok, bool inSwitch, bool hasIfdef); void unusedLabelError(const Token* tok, bool inSwitch, bool hasIfdef);
void unknownEvaluationOrder(const Token* tok); void unknownEvaluationOrder(const Token* tok);
@ -343,7 +343,7 @@ private:
c.varFuncNullUBError(nullptr); c.varFuncNullUBError(nullptr);
c.nanInArithmeticExpressionError(nullptr); c.nanInArithmeticExpressionError(nullptr);
c.commaSeparatedReturnError(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, false);
c.unusedLabelError(nullptr, false, true); c.unusedLabelError(nullptr, false, true);
c.unusedLabelError(nullptr, true, false); c.unusedLabelError(nullptr, true, false);

View File

@ -8764,6 +8764,16 @@ private:
"}\n", nullptr, false, true); "}\n", nullptr, false, true);
ASSERT_EQUALS("[test.cpp:2]: (style) Redundant pointer operation on 'y' - it's already a pointer.\n", errout.str()); 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 // no warning for bitwise AND
check("void f(const int *b) {\n" check("void f(const int *b) {\n"
" int x = 0x20 & *b;\n" " int x = 0x20 & *b;\n"