Skip to content

Commit 529e6ac

Browse files
committed
Fix #15144 FN knownConditionTrueFalse (binary or)
1 parent 275f16a commit 529e6ac

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,10 @@ void CheckConditionImpl::checkBadBitmaskCheck()
327327
const bool isTrue = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->getKnownIntValue() != 0) ||
328328
(tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->getKnownIntValue() != 0);
329329

330-
if (isBoolean && isTrue)
330+
if (isBoolean && isTrue) {
331331
badBitmaskCheckError(tok);
332+
diag(tok);
333+
}
332334

333335
// If there are #ifdef in the expression don't warn about redundant | to avoid FP
334336
const auto& startStop = tok->findExpressionStartEndTokens();

‎lib/valueflow.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
10961096
value.setImpossible();
10971097
setTokenValue(tok, std::move(value), settings);
10981098
}
1099-
if (Token::simpleMatch(tok, "?") && Token::Match(tok->astOperand1(), "<|<=|>|>=")) {
1099+
else if (Token::simpleMatch(tok, "?") && Token::Match(tok->astOperand1(), "<|<=|>|>=")) {
11001100
const Token* condTok = tok->astOperand1();
11011101
const Token* branchesTok = tok->astOperand2();
11021102

@@ -1139,11 +1139,20 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
11391139
setTokenValue(tok, std::move(value), settings);
11401140
}
11411141

1142-
} else if (Token::simpleMatch(tok, "%") && tok->astOperand2() && tok->astOperand2()->hasKnownIntValue()) {
1142+
} else if (tok->isBinaryOp() && tok->str() == "%" && tok->astOperand2()->hasKnownIntValue()) {
11431143
ValueFlow::Value value{tok->astOperand2()->getKnownIntValue()};
11441144
value.bound = ValueFlow::Value::Bound::Lower;
11451145
value.setImpossible();
11461146
setTokenValue(tok, std::move(value), settings);
1147+
} else if (tok->isBinaryOp() && tok->str() == "|") {
1148+
const ValueFlow::Value* opVal = tok->astOperand1()->getKnownValue(ValueFlow::Value::ValueType::INT);
1149+
if (!opVal)
1150+
opVal = tok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT);
1151+
if (opVal && opVal->intvalue != 0) {
1152+
ValueFlow::Value value{0};
1153+
value.setImpossible();
1154+
setTokenValue(tok, std::move(value), settings);
1155+
}
11471156
} else if (Token::Match(tok, "abs|labs|llabs|fabs|fabsf|fabsl (")) {
11481157
ValueFlow::Value value{-1};
11491158
value.bound = ValueFlow::Value::Bound::Upper;

‎test/testcondition.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4999,6 +4999,13 @@ class TestCondition : public TestFixture {
49994999
"[test.cpp:9:9]: (style) Condition '0b101010' is always true [knownConditionTrueFalse]\n"
50005000
"[test.cpp:10:9]: (style) Condition '!42' is always false [knownConditionTrueFalse]\n",
50015001
errout_str());
5002+
5003+
check("void f(int i) {\n" // #15144
5004+
" int c = i | 1;\n"
5005+
" if (c) {}\n"
5006+
"}\n");
5007+
ASSERT_EQUALS("[test.cpp:3:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n",
5008+
errout_str());
50025009
}
50035010

50045011
void alwaysTrueSymbolic()

‎test/testvalueflow.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TestValueFlow : public TestFixture {
5757
TEST_CASE(valueFlowMove);
5858

5959
TEST_CASE(valueFlowBitAnd);
60+
TEST_CASE(valueFlowBitOr);
6061
TEST_CASE(valueFlowRightShift);
6162

6263
TEST_CASE(valueFlowCalculations);
@@ -4416,6 +4417,16 @@ class TestValueFlow : public TestFixture {
44164417
ASSERT_EQUALS(false, testValueOfX(code,3U,16));
44174418
}
44184419

4420+
void valueFlowBitOr() {
4421+
const char *code;
4422+
4423+
code = "int f(int a) {\n"
4424+
" int x = a | 0x80;\n"
4425+
" return x;\n"
4426+
"}\n";
4427+
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, 0));
4428+
}
4429+
44194430
void valueFlowForwardInconclusiveImpossible() {
44204431
const char *code;
44214432

0 commit comments

Comments
 (0)