Merge pull request #785 from bartlomiejgrzeskowiak/master

#7428 assigned enhancement
This commit is contained in:
amai2012 2016-04-14 23:15:53 +02:00
commit 335c5a9c5a
2 changed files with 42 additions and 5 deletions

View File

@ -281,7 +281,7 @@ void CheckCondition::comparison()
return; return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "==|!=")) { if (Token::Match(tok, "==|!=|>|>=|<|<=")) {
const Token *expr1 = tok->astOperand1(); const Token *expr1 = tok->astOperand1();
const Token *expr2 = tok->astOperand2(); const Token *expr2 = tok->astOperand2();
if (!expr1 || !expr2) if (!expr1 || !expr2)
@ -301,10 +301,24 @@ void CheckCondition::comparison()
const MathLib::bigint num1 = *num; const MathLib::bigint num1 = *num;
if (num1 < 0) if (num1 < 0)
continue; continue;
if ((expr1->str() == "&" && (num1 & num2) != num2) || if (Token::Match(tok, "==|!=|>=|<=")) {
(expr1->str() == "|" && (num1 | num2) != num2)) { if ((expr1->str() == "&" && (num1 & num2) != num2) ||
const std::string& op(tok->str()); (expr1->str() == "|" && (num1 | num2) != num2)) {
comparisonError(expr1, expr1->str(), num1, op, num2, op=="==" ? false : true); const std::string& op(tok->str());
comparisonError(expr1, expr1->str(), num1, op, num2, op!="!=" ? false : true);
}
}
else if (Token::simpleMatch(tok, ">")) {
if ((expr1->str() == "&" && (num1 <= num2))) {
const std::string& op(tok->str());
comparisonError(expr1, expr1->str(), num1, op, num2, false);
}
}
else if (Token::simpleMatch(tok, "<")) {
if ((expr1->str() == "|" && (num1 >= num2))) {
const std::string& op(tok->str());
comparisonError(expr1, expr1->str(), num1, op, num2, false);
}
} }
} }
} }

View File

@ -1622,6 +1622,29 @@ private:
" assert(x == 0);\n" " assert(x == 0);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
//TRAC #7428 false negative: Statement is always false
check("void f() {\n"
"assert( (a & 0x07) == 8U );\n" // statement always false, because 7 == 8 is false
"assert( (a & 0x07) > 7U );\n" // statement always false, because 7 > 7 is false
"assert( (a | 0x07) < 7U );\n" // statement always false, because 7 < 7 is false
"assert( (a & 0x07) > 8U );\n" // statement always false, because 7 > 8 is false
"assert( (a | 0x07) < 6U );\n" // statement always false, because 7 < 6 is false
"assert( (a & 0x07) >= 7U );\n" // statement correct
"assert( (a | 0x07) <= 7U );\n" // statement correct
"assert( (a & 0x07) >= 8U );\n" // statement always false, because 7 >= 8 is false
"assert( (a | 0x07) <= 6U );\n" // statement always false, because 7 <= 6 is false
"assert( (a & 0x07) > 3U );\n" // statement correct
"assert( (a | 0x07) < 9U );\n" // statement correct
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n"
"[test.cpp:3]: (style) Expression '(X & 0x7) > 0x7' is always false.\n"
"[test.cpp:4]: (style) Expression '(X | 0x7) < 0x7' is always false.\n"
"[test.cpp:5]: (style) Expression '(X & 0x7) > 0x8' is always false.\n"
"[test.cpp:6]: (style) Expression '(X | 0x7) < 0x6' is always false.\n"
"[test.cpp:9]: (style) Expression '(X & 0x7) >= 0x8' is always false.\n"
"[test.cpp:10]: (style) Expression '(X | 0x7) <= 0x6' is always false.\n",
errout.str());
} }
void checkInvalidTestForOverflow() { void checkInvalidTestForOverflow() {