diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 2e310e9c0..8b13fe127 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1807,7 +1807,9 @@ void CheckCondition::checkCompareValueOutOfTypeRange() const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (-(1LL << (bits-1))) : 0; const auto unsignedTypeMaxValue = (1LL << bits) - 1LL; - const auto typeMaxValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (unsignedTypeMaxValue / 2) : unsignedTypeMaxValue; + const auto typeMaxValue = (typeTok->valueType()->sign != ValueType::Sign::SIGNED || bits >= mSettings->int_bit) ? + unsignedTypeMaxValue : // unsigned type. signed int/long/long long; comparing sign bit is ok. i.e. 'i == 0xffffffff' + (unsignedTypeMaxValue / 2); // signed char/short if (valueTok->getKnownIntValue() < typeMinValue) compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), valueTok->getKnownIntValue()); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 075ae52a1..fbf7932f8 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4439,6 +4439,32 @@ private: " if (b == true) {}\n" "}", &settingsUnix64); ASSERT_EQUALS("", errout.str()); + + // #10372 + check("void f(signed char x) {\n" + " if (x == 0xff) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed char' against value 255. Condition is always true/false.\n", errout.str()); + + check("void f(short x) {\n" + " if (x == 0xffff) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed short' against value 65535. Condition is always true/false.\n", errout.str()); + + check("void f(int x) {\n" + " if (x == 0xffffffff) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("", errout.str()); + + check("void f(long x) {\n" + " if (x == ~0L) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("", errout.str()); + + check("void f(long long x) {\n" + " if (x == ~0LL) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("", errout.str()); } void knownConditionCast() { // #9976