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
23 changes: 21 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ static void valueFlowImpossibleValues(TokenList& tokenList, const Settings& sett
value.setImpossible();
setTokenValue(tok, std::move(value), settings);
}
if (Token::simpleMatch(tok, "?") && Token::Match(tok->astOperand1(), "<|<=|>|>=")) {
else if (Token::simpleMatch(tok, "?") && Token::Match(tok->astOperand1(), "<|<=|>|>=")) {

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

This else causes a regression. An unsigned ?: token matches the preceding astIsUnsigned(tok) && !astIsPointer(tok) branch, so the min/max handling below is skipped for it. Example: unsigned f(unsigned a, unsigned b) { unsigned m = a < b ? a : b; if (m > a) {} return m; } reports Condition 'm>a' is always false on base but nothing with this PR. The signed version is still reported. #15144 doesn't need this change, so I suggest reverting it to if.

const Token* condTok = tok->astOperand1();
const Token* branchesTok = tok->astOperand2();

Expand Down Expand Up @@ -1139,11 +1139,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 @@ -4999,6 +4999,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
11 changes: 11 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 @@ -4416,6 +4417,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