Skip to content

Commit a5cfa2b

Browse files
- cppcheck-opensource#7522 and cppcheck-opensource#7428 revisited. ((a&7)>7U) is always false and ((X|7)>=6) is correct (X can be negative).
1 parent 7466a49 commit a5cfa2b

2 files changed

Lines changed: 72 additions & 31 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void CheckCondition::comparison()
281281
return;
282282

283283
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
284-
if (Token::Match(tok, "==|!=|>|>=|<|<=")) {
284+
if (Token::Match(tok, "==|!=|>")) {
285285
const Token *expr1 = tok->astOperand1();
286286
const Token *expr2 = tok->astOperand2();
287287
if (!expr1 || !expr2)
@@ -301,22 +301,17 @@ void CheckCondition::comparison()
301301
const MathLib::bigint num1 = *num;
302302
if (num1 < 0)
303303
continue;
304-
if (Token::Match(tok, "==|!=|>=|<=")) {
304+
if (Token::Match(tok, "==|!=")) {
305305
if ((expr1->str() == "&" && (num1 & num2) != num2) ||
306306
(expr1->str() == "|" && (num1 | num2) != num2)) {
307307
const std::string& op(tok->str());
308-
comparisonError(expr1, expr1->str(), num1, op, num2, op!="!=" ? false : true);
308+
comparisonError(expr1, expr1->str(), num1, op, num2, op=="==" ? false : true);
309309
}
310310
} else if (Token::simpleMatch(tok, ">")) {
311311
if ((expr1->str() == "&" && (num1 <= num2))) {
312312
const std::string& op(tok->str());
313313
comparisonError(expr1, expr1->str(), num1, op, num2, false);
314314
}
315-
} else if (Token::simpleMatch(tok, "<")) {
316-
if ((expr1->str() == "|" && (num1 >= num2))) {
317-
const std::string& op(tok->str());
318-
comparisonError(expr1, expr1->str(), num1, op, num2, false);
319-
}
320315
}
321316
}
322317
}

‎test/testcondition.cpp‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,75 @@ class TestCondition : public TestFixture {
591591
" }\n"
592592
"}");
593593
ASSERT_EQUALS("", errout.str());
594+
595+
//Various bitmask comparison checks
596+
//#7428 false negative: condition '(a&7)>7U' is always false
597+
//#7522 false positive: condition '(X|7)>=6' is correct
598+
599+
check("void f() {\n"
600+
"assert( (a & 0x07) == 8U );\n" // statement always false
601+
"}");
602+
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n",
603+
errout.str());
604+
605+
check("void f() {\n"
606+
"assert( (a & 0x07) == 7U );\n" // statement correct
607+
"assert( (a & 0x07) == 6U );\n" // statement correct
608+
"}");
609+
ASSERT_EQUALS("", errout.str());
610+
611+
check("void f() {\n"
612+
"assert( (a | 0x07) == 8U );\n" // statement always false
613+
"}");
614+
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) == 0x8' is always false.\n",
615+
errout.str());
616+
617+
check("void f() {\n"
618+
"assert( (a | 0x07) == 7U );\n" // statement correct
619+
"assert( (a | 0x07) == 23U );\n" // statement correct
620+
"}");
621+
ASSERT_EQUALS("", errout.str());
622+
623+
check("void f() {\n"
624+
"assert( (a & 0x07) != 8U );\n" // statement always true
625+
"}");
626+
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) != 0x8' is always true.\n",
627+
errout.str());
628+
629+
check("void f() {\n"
630+
"assert( (a & 0x07) != 7U );\n" // statement correct
631+
"assert( (a & 0x07) != 0U );\n" // statement correct
632+
"}");
633+
ASSERT_EQUALS("", errout.str());
634+
635+
check("void f() {\n"
636+
"assert( (a | 0x07) != 8U );\n" // statement always true
637+
"}");
638+
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X | 0x7) != 0x8' is always true.\n",
639+
errout.str());
640+
641+
check("void f() {\n"
642+
"assert( (a | 0x07) != 7U );\n" // statement correct
643+
"}");
644+
ASSERT_EQUALS("", errout.str());
645+
646+
//TRAC #7428 false negative: condition '(X&7)>7' is always false
647+
check("void f() {\n"
648+
"assert( (a & 0x07) > 7U );\n" // statement always false
649+
"}");
650+
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) > 0x7' is always false.\n",
651+
errout.str());
652+
653+
check("void f() {\n"
654+
"assert( (a & 0x07) > 6U );\n" // statement correct
655+
"}");
656+
ASSERT_EQUALS("", errout.str());
657+
658+
//TRAC #7522 false positive: condition '(X|7)>=6' is correct (X can be negative)
659+
check("void f() {\n"
660+
"assert( (a | 0x07) >= 6U );\n" // statement correct (X can be negative)
661+
"}");
662+
ASSERT_EQUALS("",errout.str());
594663
}
595664

596665

@@ -1651,29 +1720,6 @@ class TestCondition : public TestFixture {
16511720
" assert(x == 0);\n"
16521721
"}");
16531722
ASSERT_EQUALS("", errout.str());
1654-
1655-
//TRAC #7428 false negative: Statement is always false
1656-
check("void f() {\n"
1657-
"assert( (a & 0x07) == 8U );\n" // statement always false, because 7 == 8 is false
1658-
"assert( (a & 0x07) > 7U );\n" // statement always false, because 7 > 7 is false
1659-
"assert( (a | 0x07) < 7U );\n" // statement always false, because 7 < 7 is false
1660-
"assert( (a & 0x07) > 8U );\n" // statement always false, because 7 > 8 is false
1661-
"assert( (a | 0x07) < 6U );\n" // statement always false, because 7 < 6 is false
1662-
"assert( (a & 0x07) >= 7U );\n" // statement correct
1663-
"assert( (a | 0x07) <= 7U );\n" // statement correct
1664-
"assert( (a & 0x07) >= 8U );\n" // statement always false, because 7 >= 8 is false
1665-
"assert( (a | 0x07) <= 6U );\n" // statement always false, because 7 <= 6 is false
1666-
"assert( (a & 0x07) > 3U );\n" // statement correct
1667-
"assert( (a | 0x07) < 9U );\n" // statement correct
1668-
"}");
1669-
ASSERT_EQUALS("[test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n"
1670-
"[test.cpp:3]: (style) Expression '(X & 0x7) > 0x7' is always false.\n"
1671-
"[test.cpp:4]: (style) Expression '(X | 0x7) < 0x7' is always false.\n"
1672-
"[test.cpp:5]: (style) Expression '(X & 0x7) > 0x8' is always false.\n"
1673-
"[test.cpp:6]: (style) Expression '(X | 0x7) < 0x6' is always false.\n"
1674-
"[test.cpp:9]: (style) Expression '(X & 0x7) >= 0x8' is always false.\n"
1675-
"[test.cpp:10]: (style) Expression '(X | 0x7) <= 0x6' is always false.\n",
1676-
errout.str());
16771723
}
16781724

16791725
void checkInvalidTestForOverflow() {

0 commit comments

Comments
 (0)