Skip to content

Commit 94218bd

Browse files
Fix #13360 FN comparisonError with bit mask (regression) (cppcheck-opensource#7051)
1 parent 4a0da3a commit 94218bd

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lib/checkcondition.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ void CheckCondition::comparison()
368368
const Token *expr2 = tok->astOperand2();
369369
if (!expr1 || !expr2)
370370
continue;
371-
if (expr1->isNumber())
371+
if (expr1->hasKnownIntValue())
372372
std::swap(expr1,expr2);
373-
if (!expr2->isNumber())
373+
if (!expr2->hasKnownIntValue())
374374
continue;
375375
if (!compareTokenFlags(expr1, expr2, /*macro*/ true))
376376
continue;
377-
const MathLib::bigint num2 = MathLib::toBigNumber(expr2->str());
377+
const MathLib::bigint num2 = expr2->getKnownIntValue();
378378
if (num2 < 0)
379379
continue;
380380
if (!Token::Match(expr1,"[&|]"))

test/testcondition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,15 @@ class TestCondition : public TestFixture {
503503
" if (MACRO_ALL == 0) {}\n"
504504
"}\n");
505505
ASSERT_EQUALS("", errout_str());
506+
507+
checkP("void f(int i, int j) {\n" // #13360
508+
" int X = 0x10;\n"
509+
" if ((i & 0xff00) == X) {}\n"
510+
" if (X == (j & 0xff00)) {}\n"
511+
"}\n");
512+
ASSERT_EQUALS("[test.cpp:3]: (style) Expression '(X & 0xff00) == 0x10' is always false.\n"
513+
"[test.cpp:4]: (style) Expression '(X & 0xff00) == 0x10' is always false.\n",
514+
errout_str());
506515
}
507516

508517
#define checkPureFunction(code) checkPureFunction_(code, __FILE__, __LINE__)

0 commit comments

Comments
 (0)