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() == "(")
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()

View File

@ -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);

View File

@ -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"