diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 27999245a..f37a9c36b 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -51,6 +51,13 @@ namespace { CheckCondition instance; } +bool CheckCondition::isAliased(const std::set &vars) const { + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { + if (Token::Match(tok, "= & %var% ;") && vars.find(tok->tokAt(2)->varId()) != vars.end()) + return true; + } + return false; +} void CheckCondition::assignIf() { @@ -516,11 +523,15 @@ void CheckCondition::multiCondition2() const Token *cond2 = tok->next()->astOperand2(); if (type == MULTICONDITIONTYPE::INNER) { - if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true)) - oppositeInnerConditionError(cond1, cond2); + if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true)) { + if (!isAliased(vars)) + oppositeInnerConditionError(cond1, cond2); + } } else { - if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true)) - sameConditionAfterEarlyExitError(cond1, cond2); + if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true)) { + if (!isAliased(vars)) + sameConditionAfterEarlyExitError(cond1, cond2); + } } } diff --git a/lib/checkcondition.h b/lib/checkcondition.h index cf8a135bc..ff077b4f6 100644 --- a/lib/checkcondition.h +++ b/lib/checkcondition.h @@ -114,7 +114,7 @@ public: void checkInvalidTestForOverflow(); private: - + bool isAliased(const std::set &vars) const; bool isOverlappingCond(const Token * const cond1, const Token * const cond2, bool pure) const; void assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result); void mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 8cce0d1e6..ad94819cf 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -76,6 +76,7 @@ private: TEST_CASE(modulo); TEST_CASE(oppositeInnerCondition); + TEST_CASE(oppositeInnerConditionAlias); TEST_CASE(oppositeInnerCondition2); TEST_CASE(oppositeInnerConditionAnd); @@ -1597,6 +1598,20 @@ private: ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str()); } + void oppositeInnerConditionAlias() { + check("void f() {\n" + " struct S s;\n" + " bool hasFailed = false;\n" + " s.status = &hasFailed;\n" + "\n" + " if (! hasFailed) {\n" + " doStuff(&s);\n" + " if (hasFailed) {}\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void oppositeInnerCondition2() { // first comparison: < check("void f(int x) {\n"