Skip to content

Commit

Permalink
Fix #12874 FP redundantAssignment with offset (#6555)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jun 29, 2024
1 parent a8ee10a commit fc9bd28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ void CheckOther::checkRedundantAssignment()
}

if (start->hasKnownSymbolicValue(tokenToCheck) && Token::simpleMatch(start->astParent(), "=") && !diag(tok)) {
redundantAssignmentSameValueError(start, tokenToCheck, tok->astOperand1()->expressionString());
const ValueFlow::Value* val = start->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC);
if (val->intvalue == 0) // no offset
redundantAssignmentSameValueError(tokenToCheck, val, tok->astOperand1()->expressionString());
}

// Get next assignment..
Expand Down Expand Up @@ -593,11 +595,10 @@ void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token
"Variable '$symbol' is reassigned a value before the old one has been used. 'break;' missing?", CWE563, Certainty::normal);
}

void CheckOther::redundantAssignmentSameValueError(const Token *tok1, const Token* tok2, const std::string &var)
void CheckOther::redundantAssignmentSameValueError(const Token *tok, const ValueFlow::Value* val, const std::string &var)
{
const ValueFlow::Value* val = tok1->getKnownValue(ValueFlow::Value::ValueType::SYMBOLIC);
auto errorPath = val->errorPath;
errorPath.emplace_back(tok2, "");
errorPath.emplace_back(tok, "");
reportError(errorPath, Severity::style, "redundantAssignment",
"$symbol:" + var + "\n"
"Variable '$symbol' is assigned an expression that holds the same value.", CWE563, Certainty::normal);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class CPPCHECKLIB CheckOther : public Check {
void redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
void redundantInitializationError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
void redundantAssignmentInSwitchError(const Token *tok1, const Token *tok2, const std::string &var);
void redundantAssignmentSameValueError(const Token *tok1, const Token *tok2, const std::string &var);
void redundantAssignmentSameValueError(const Token* tok, const ValueFlow::Value* val, const std::string& var);
void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var);
void redundantBitwiseOperationInSwitchError(const Token *tok, const std::string &varname);
void suspiciousCaseInSwitchError(const Token* tok, const std::string& operatorString);
Expand Down
8 changes: 8 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10217,6 +10217,14 @@ class TestOther : public TestFixture {
" return a * b * c;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Redundant initialization for 'b'. The initialized value is overwritten before it is read.\n", errout_str());

check("int f(int i) {\n" // #12874
" int j = i + 1;\n"
" if (i > 5)\n"
" j = i;\n"
" return j;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void varFuncNullUB() { // #4482
Expand Down

0 comments on commit fc9bd28

Please sign in to comment.