From 797de4ef920dda5c569e5fd74e27731bc32053dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 16 Jul 2021 19:08:08 +0200 Subject: [PATCH] Fixed #10363 (FP: compareValueOutOfTypeRangeError) --- lib/checkcondition.cpp | 2 +- test/testcondition.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) 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()); } };