CheckCondition: Improved checking for same conditions

This commit is contained in:
Daniel Marjamäki 2017-09-04 22:40:34 +02:00
parent 5885988b1f
commit d82805b7fe
2 changed files with 32 additions and 14 deletions

View File

@ -533,9 +533,20 @@ void CheckCondition::multiCondition2()
oppositeInnerConditionError(cond1, cond2);
}
} else {
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true)) {
if (!isAliased(vars))
sameConditionAfterEarlyExitError(cond1, cond2);
std::stack<const Token *> tokens2;
tokens2.push(cond2);
while (!tokens2.empty()) {
const Token *secondCondition = tokens2.top();
tokens2.pop();
if (!secondCondition)
continue;
if (secondCondition->str() == "||") {
tokens2.push(secondCondition->astOperand1());
tokens2.push(secondCondition->astOperand2());
} else if (isSameExpression(_tokenizer->isCPP(), true, cond1, secondCondition, _settings->library, true)) {
if (!isAliased(vars))
sameConditionAfterEarlyExitError(cond1, secondCondition);
}
}
}
}
@ -606,10 +617,11 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token*
void CheckCondition::sameConditionAfterEarlyExitError(const Token *cond1, const Token* cond2)
{
const std::string cond(cond1 ? cond1->expressionString() : "x");
ErrorPath errorPath;
errorPath.push_back(ErrorPathItem(cond1, "first condition"));
errorPath.push_back(ErrorPathItem(cond2, "second condition"));
reportError(errorPath, Severity::warning, "sameConditionAfterEarlyExit", "Same condition, second condition is always false", CWE398, false);
reportError(errorPath, Severity::warning, "sameConditionAfterEarlyExit", "Same condition '" + cond + "', second condition is always false", CWE398, false);
}
//---------------------------------------------------------------------------

View File

@ -1513,13 +1513,6 @@ private:
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(const int *i) {\n"
" if (!i) return;\n"
" if (!num1tok) { *num1 = *num2; }\n"
" if (!i) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
}
void oppositeInnerConditionClass() {
@ -1761,21 +1754,34 @@ private:
" if (x > 100) { return; }\n"
" if (x > 100) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition, second condition is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
check("void f(int x) {\n"
" if (x > 100) { return; }\n"
" if (x > 100 || y > 100) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
check("void f(int x) {\n"
" if (x > 100) { return; }\n"
" if (abc) {}\n"
" if (x > 100) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
check("void f(int x) {\n"
" if (x > 100) { return; }\n"
" while (abc) { y = x; }\n"
" if (x > 100) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition, second condition is always false\n", errout.str());
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition 'x>100', second condition is always false\n", errout.str());
check("void f(const int *i) {\n"
" if (!i) return;\n"
" if (!num1tok) { *num1 = *num2; }\n"
" if (!i) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Same condition '!i', second condition is always false\n", errout.str());
}
// clarify conditions with = and comparison