Fix #9789 FP Logical condition considered always false (#3985)

This commit is contained in:
chrchr-github 2022-04-09 14:09:10 +02:00 committed by GitHub
parent 8b76a109ce
commit 2bccde0e3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -1134,6 +1134,11 @@ void CheckCondition::checkIncorrectLogicOperator()
if (inconclusive && !printInconclusive)
continue;
const bool isUnknown = (expr1 && expr1->valueType() && expr1->valueType()->type == ValueType::UNKNOWN_TYPE) ||
(expr2 && expr2->valueType() && expr2->valueType()->type == ValueType::UNKNOWN_TYPE);
if (isUnknown)
continue;
const bool isfloat = astIsFloat(expr1, true) || MathLib::isFloat(value1) || astIsFloat(expr2, true) || MathLib::isFloat(value2);
ErrorPath errorPath;
@ -1442,6 +1447,12 @@ void CheckCondition::alwaysTrueFalse()
if (!(constIfWhileExpression || constValExpr || compExpr || ternaryExpression || returnExpression))
continue;
const Token* expr1 = tok->astOperand1(), *expr2 = tok->astOperand2();
const bool isUnknown = (expr1 && expr1->valueType() && expr1->valueType()->type == ValueType::UNKNOWN_TYPE) ||
(expr2 && expr2->valueType() && expr2->valueType()->type == ValueType::UNKNOWN_TYPE);
if (isUnknown)
continue;
// Don't warn when there are expanded macros..
bool isExpandedMacro = false;
visitAstNodes(tok, [&](const Token * tok2) {

View File

@ -1209,6 +1209,14 @@ private:
" if (x && x != ZERO) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f(int N) {\n" // #9789
" T a[20] = { 0 };\n"
" for (int i = 0; i < N; ++i) {\n"
" if (0 < a[i] && a[i] < 1) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void incorrectLogicOperator5() { // complex expressions