Skip to content
Draft
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: 2 additions & 2 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,6 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
continue;
if (valueTok->getKnownIntValue() < 0 && valueTok->valueType() && valueTok->valueType()->sign != ValueType::Sign::SIGNED)
continue;
if (valueTok->valueType() && valueTok->valueType()->isTypeEqual(typeTok->valueType()))
continue;
std::uint8_t bits = 0;
switch (typeTok->valueType()->type) {
case ValueType::Type::BOOL:
Expand Down Expand Up @@ -2015,6 +2013,8 @@ void CheckCondition::checkCompareValueOutOfTypeRange()

bool result{};
const auto kiv = valueTok->getKnownIntValue();
if (kiv == 0)
continue; // prevent overlap with TestOther::unsignedPositive/unsignedLessThanZero
if (tok->str() == "==")
result = false;
else if (tok->str() == "!=")
Expand Down
4 changes: 2 additions & 2 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ namespace ValueFlow
return value;
if (!parent->isBinaryOp())
return value;
if (!parent->isConstOp())
if (!parent->isConstOp() && !parent->isAssignmentOp())
return value;
if (!astIsIntegral(parent->astOperand1(), false))
return value;
Expand All @@ -88,7 +88,7 @@ namespace ValueFlow
ValueType::Sign sign = ValueType::Sign::UNSIGNED;
if (n1 < n2)
sign = vt2->sign;
else if (n1 > n2)
else if (n1 >= n2)
sign = vt1->sign;
Value v = castValue(value, sign, std::max(n1, n2) * 8);
v.wideintvalue = value.intvalue;
Expand Down
11 changes: 10 additions & 1 deletion test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4065,7 +4065,7 @@ class TestCondition : public TestFixture {
" unsigned int num = max - 1;\n"
" if (num < 0) {}\n" // <- do not report knownConditionTrueFalse
"}");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:3:15]: (style) Comparing expression of type 'unsigned int' against value 0. Condition is always false. [compareValueOutOfTypeRangeError]\n", errout_str());

// #10297
check("void foo(size_t len, int start) {\n"
Expand Down Expand Up @@ -6355,6 +6355,15 @@ class TestCondition : public TestFixture {
"}\n", settingsUnix64);
ASSERT_EQUALS("[test.cpp:2:14]: (style) Comparing expression of type 'bool' against value 2. Condition is always true. [compareValueOutOfTypeRangeError]\n",
errout_str());

check("void f(const std::uint32_t& u) {\n" // #9078
" if (u >= UINT32_MAX) {}\n"
" if (u <= UINT32_MAX) {}\n"
" if (u > UINT32_MAX) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:14]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always true. [compareValueOutOfTypeRangeError]\n"
"[test.cpp:4:13]: (style) Comparing expression of type 'const unsigned int &' against value 4294967295. Condition is always false. [compareValueOutOfTypeRangeError]\n",
errout_str());
}

void knownConditionCast() {
Expand Down
18 changes: 18 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9313,6 +9313,24 @@ class TestOther : public TestFixture {
" for (unsigned p = 0; p < (sizeof(a) / sizeof((a)[0])); ++p) {}\n"
"}");
ASSERT_EQUALS("", errout_str());

check("void f(const unsigned char u) {\n"
" if (u > 0) {}\n"
" if (u < 0) {}\n"
" if (u >= 0) {}\n"
" if (u <= 0) {}\n"
" if (0 < u) {}\n"
" if (0 > u) {}\n"
" if (0 <= u) {}\n"
" if (0 >= u) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:3:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n"
"[test.cpp:4:11]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n"
"[test.cpp:5:11]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n"
"[test.cpp:7:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n"
"[test.cpp:8:13]: (style) Unsigned expression 'u' can't be negative so it is unnecessary to test it. [unsignedPositive]\n"
"[test.cpp:9:13]: (style) Checking if unsigned expression 'u' is less than zero. [unsignedLessThanZero]\n",
errout_str());
}

void checkSignOfPointer() {
Expand Down
Loading