Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ void CheckConditionImpl::checkBadBitmaskCheck()
const bool isTrue = (tok->astOperand1()->hasKnownIntValue() && tok->astOperand1()->getKnownIntValue() != 0) ||
(tok->astOperand2()->hasKnownIntValue() && tok->astOperand2()->getKnownIntValue() != 0);

if (isBoolean && isTrue)
if (isBoolean && isTrue) {
badBitmaskCheckError(tok);
diag(tok);
}

// If there are #ifdef in the expression don't warn about redundant | to avoid FP
const auto& startStop = tok->findExpressionStartEndTokens();
Expand Down
21 changes: 20 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,11 +1089,30 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
setTokenValue(tok, std::move(value), settings);
}

} else if (Token::simpleMatch(tok, "%") && tok->astOperand2() && tok->astOperand2()->hasKnownIntValue()) {
} else if (tok->isBinaryOp() && tok->str() == "%" && tok->astOperand2()->hasKnownIntValue()) {
ValueFlow::Value value{tok->astOperand2()->getKnownIntValue()};
value.bound = ValueFlow::Value::Bound::Lower;
value.setImpossible();
setTokenValue(tok, std::move(value), settings);
} else if (tok->isBinaryOp() && tok->str() == "|") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The impossible 0 is kept when the result is converted to a narrower type, so these are now reported as always true although they can be 0: unsigned char c = i | 0x100; if (c) {}, if ((unsigned char)(i | 0x100)) {} and long long i; int c = i | 0x100000000LL; if (c) {}. Base already has this weakness for a plain != 0 value that is narrowed, but x | FLAG is a common pattern, so it would help to only set the value when a known non-zero operand fits in the type it is converted to (or at least the lower bits).

bool nonNull = false;
for (const Token *op : {tok->astOperand1(), tok->astOperand2()}) {
for (const ValueFlow::Value& v : op->values()) {
if (v.valueType != ValueFlow::Value::ValueType::INT)
continue;
if ((v.isKnown() && v.intvalue != 0) || (v.isImpossible() && v.intvalue == 0)) {
nonNull = true;
break;
}
}
if (nonNull)
break;
}
if (nonNull) {
ValueFlow::Value value{0};
value.setImpossible();
setTokenValue(tok, std::move(value), settings);
}
} else if (Token::Match(tok, "abs|labs|llabs|fabs|fabsf|fabsl (")) {
ValueFlow::Value value{-1};
value.bound = ValueFlow::Value::Bound::Upper;
Expand Down
14 changes: 14 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5011,6 +5011,20 @@ class TestCondition : public TestFixture {
"[test.cpp:9:9]: (style) Condition '0b101010' is always true [knownConditionTrueFalse]\n"
"[test.cpp:10:9]: (style) Condition '!42' is always false [knownConditionTrueFalse]\n",
errout_str());

check("void f(int i) {\n" // #15144
" int c = i | 1;\n"
" if (c) {}\n"
"}\n"
"void g(int i, int m) {\n"
" if (m == 0)\n"
" return;\n"
" int c = i | m;\n"
" if (c) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n"
"[test.cpp:9:9]: (style) Condition 'c' is always true [knownConditionTrueFalse]\n",
errout_str());
}

void alwaysTrueSymbolic()
Expand Down
18 changes: 18 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TestValueFlow : public TestFixture {
TEST_CASE(valueFlowMove);

TEST_CASE(valueFlowBitAnd);
TEST_CASE(valueFlowBitOr);
TEST_CASE(valueFlowRightShift);

TEST_CASE(valueFlowCalculations);
Expand Down Expand Up @@ -4332,6 +4333,13 @@ class TestValueFlow : public TestFixture {
"}\n";
auto values = tokenValues(code, "s :", ValueFlow::Value::ValueType::FLOAT);
ASSERT_EQUALS(0, values.size());

code = "bool f(unsigned a, unsigned b) {\n"
" unsigned m = a < b ? a : b;\n"
" bool x = (m > a);\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfX(code, 4U, 0));
}

void valueFlowForwardLambda() {
Expand Down Expand Up @@ -4423,6 +4431,16 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(false, testValueOfX(code,3U,16));
}

void valueFlowBitOr() {
const char *code;

code = "int f(int a) {\n"
" int x = a | 0x80;\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(true, testValueOfXImpossible(code, 3U, 0));
}

void valueFlowForwardInconclusiveImpossible() {
const char *code;

Expand Down
Loading