Fixed #10363 (FP: compareValueOutOfTypeRangeError)

This commit is contained in:
Daniel Marjamäki 2021-07-16 19:08:08 +02:00
parent 65a6d4b45c
commit 797de4ef92
2 changed files with 13 additions and 3 deletions

View File

@ -1806,7 +1806,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
continue; continue;
const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (-(1LL << (bits-1))) : 0; 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; const auto typeMaxValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (unsignedTypeMaxValue / 2) : unsignedTypeMaxValue;
if (valueTok->getKnownIntValue() < typeMinValue) if (valueTok->getKnownIntValue() < typeMinValue)

View File

@ -4304,9 +4304,19 @@ private:
settingsUnix64.platform(cppcheck::Platform::PlatformType::Unix64); settingsUnix64.platform(cppcheck::Platform::PlatformType::Unix64);
check("void f(unsigned char c) {\n" check("void f(unsigned char c) {\n"
" if (c == 1234) {}\n" " if (c == 256) {}\n"
"}", &settingsUnix64); "}", &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());
} }
}; };