Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class CPPCHECKLIB CheckOther : public Check {
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);
Expand Down Expand Up @@ -343,7 +343,7 @@ class CPPCHECKLIB CheckOther : public Check {
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);
Expand Down
10 changes: 10 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8764,6 +8764,16 @@ class TestOther : public TestFixture {
"}\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"
Expand Down