diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index b7357c38e..d62c7984e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1806,7 +1806,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange() continue; const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (-(1LL << (bits-1))) : 0; - const auto unsignedTypeMaxValue = (1LL << (bits-1)) - 1LL; + const auto unsignedTypeMaxValue = (1LL << bits) - 1LL; const auto typeMaxValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (unsignedTypeMaxValue / 2) : unsignedTypeMaxValue; if (valueTok->getKnownIntValue() < typeMinValue) diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 8425ddf2c..7503b76f0 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -4304,9 +4304,19 @@ private: settingsUnix64.platform(cppcheck::Platform::PlatformType::Unix64); check("void f(unsigned char c) {\n" - " if (c == 1234) {}\n" + " if (c == 256) {}\n" "}", &settingsUnix64); - ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 1234. Condition is always true/false.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always true/false.\n", errout.str()); + + check("void f(unsigned char c) {\n" + " if (c == 255) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("", errout.str()); + + check("void f(bool b) {\n" + " if (b == true) {}\n" + "}", &settingsUnix64); + ASSERT_EQUALS("", errout.str()); } };