Skip to content

Commit 797de4e

Browse files
committed
Fixed #10363 (FP: compareValueOutOfTypeRangeError)
1 parent 65a6d4b commit 797de4e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/checkcondition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
18061806
continue;
18071807

18081808
const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (-(1LL << (bits-1))) : 0;
1809-
const auto unsignedTypeMaxValue = (1LL << (bits-1)) - 1LL;
1809+
const auto unsignedTypeMaxValue = (1LL << bits) - 1LL;
18101810
const auto typeMaxValue = (typeTok->valueType()->sign == ValueType::Sign::SIGNED) ? (unsignedTypeMaxValue / 2) : unsignedTypeMaxValue;
18111811

18121812
if (valueTok->getKnownIntValue() < typeMinValue)

test/testcondition.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4304,9 +4304,19 @@ class TestCondition : public TestFixture {
43044304
settingsUnix64.platform(cppcheck::Platform::PlatformType::Unix64);
43054305

43064306
check("void f(unsigned char c) {\n"
4307-
" if (c == 1234) {}\n"
4307+
" if (c == 256) {}\n"
43084308
"}", &settingsUnix64);
4309-
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 1234. Condition is always true/false.\n", errout.str());
4309+
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'unsigned char' against value 256. Condition is always true/false.\n", errout.str());
4310+
4311+
check("void f(unsigned char c) {\n"
4312+
" if (c == 255) {}\n"
4313+
"}", &settingsUnix64);
4314+
ASSERT_EQUALS("", errout.str());
4315+
4316+
check("void f(bool b) {\n"
4317+
" if (b == true) {}\n"
4318+
"}", &settingsUnix64);
4319+
ASSERT_EQUALS("", errout.str());
43104320
}
43114321
};
43124322

0 commit comments

Comments
 (0)