Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1830,29 +1830,58 @@ void CheckCondition::checkCompareValueOutOfTypeRange()
else
typeMaxValue = unsignedTypeMaxValue / 2;

bool result;
bool result{};
const auto kiv = valueTok->getKnownIntValue();
if (tok->str() == "==")
result = false;
else if (tok->str() == "!=")
result = true;
else if (tok->str()[0] == '>' && i == 0)
// num > var
result = (valueTok->getKnownIntValue() > 0);
result = (kiv > 0);
else if (tok->str()[0] == '>' && i == 1)
// var > num
result = (valueTok->getKnownIntValue() < 0);
result = (kiv < 0);
else if (tok->str()[0] == '<' && i == 0)
// num < var
result = (valueTok->getKnownIntValue() < 0);
result = (kiv < 0);
else if (tok->str()[0] == '<' && i == 1)
// var < num
result = (valueTok->getKnownIntValue() > 0);
result = (kiv > 0);

if (valueTok->getKnownIntValue() < typeMinValue) {
compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), valueTok->getKnownIntValue(), result);
bool error = false;
if (kiv < typeMinValue || kiv > typeMaxValue) {
error = true;
} else {
switch (i) {
case 0: // num cmp var
if (kiv == typeMinValue) {
if (tok->str() == "<=") {
result = true;
error = true;
} else if (tok->str() == ">")
error = true;
}
else if (kiv == typeMaxValue && (tok->str() == ">=" || tok->str() == "<")) {
error = true;
}
break;
case 1: // var cmp num
if (kiv == typeMinValue) {
if (tok->str() == ">=") {
result = true;
error = true;
} else if (tok->str() == "<")
error = true;
}
else if (kiv == typeMaxValue && (tok->str() == "<=" || tok->str() == ">")) {
error = true;
}
break;
}
}
else if (valueTok->getKnownIntValue() > typeMaxValue)
compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), valueTok->getKnownIntValue(), result);
if (error)
compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), kiv, result);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/cfg/googletest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ TEST(Test, warning_in_assert_macros)
ASSERT_GE(i, i);

unsigned int u = errno;
// cppcheck-suppress unsignedPositive
// cppcheck-suppress [unsignedPositive, compareValueOutOfTypeRangeError]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - I thought the format was cppcheck-suppress id1;id2.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the manual: // cppcheck-suppress [aaaa, bbbb]

ASSERT_GE(u, 0);
// cppcheck-suppress unsignedLessThanZero
// cppcheck-suppress [unsignedLessThanZero, compareValueOutOfTypeRangeError]
ASSERT_LT(u, 0);
}
2 changes: 1 addition & 1 deletion test/cfg/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CPPCHECK="$DIR"../../cppcheck
CFG="$DIR"../../cfg/

# Cppcheck options
CPPCHECK_OPT='--check-library --enable=information --enable=style --error-exitcode=-1 --suppress=missingIncludeSystem --inline-suppr --template="{file}:{line}:{severity}:{id}:{message}"'
CPPCHECK_OPT='--check-library --platform=unix64 --enable=information --enable=style --error-exitcode=-1 --suppress=missingIncludeSystem --inline-suppr --template="{file}:{line}:{severity}:{id}:{message}"'

# Compiler settings
CXX=g++
Expand Down
42 changes: 42 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5181,6 +5181,48 @@ class TestCondition : public TestFixture {
" if (x < 3000000000) {}\n"
"}", &settingsUnix64);
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'signed int' against value 3000000000. Condition is always true.\n", errout.str());

check("void f(const signed char i) {\n" // #8545
" if (i > -129) {}\n" // warn
" if (i >= -128) {}\n" // warn
" if (i >= -127) {}\n"
" if (i < +128) {}\n" // warn
" if (i <= +127) {}\n" // warn
" if (i <= +126) {}\n"
"}\n", &settingsUnix64);
ASSERT_EQUALS("[test.cpp:2]: (style) Comparing expression of type 'const signed char' against value -129. Condition is always true.\n"
"[test.cpp:3]: (style) Comparing expression of type 'const signed char' against value -128. Condition is always true.\n"
"[test.cpp:5]: (style) Comparing expression of type 'const signed char' against value 128. Condition is always true.\n"
"[test.cpp:6]: (style) Comparing expression of type 'const signed char' against value 127. Condition is always true.\n",
errout.str());

check("void f(const unsigned char u) {\n"
" if (u > 0) {}\n"
" if (u < 0) {}\n" // warn
" if (u >= 0) {}\n" // warn
" if (u <= 0) {}\n"
" if (u > 255) {}\n" // warn
" if (u < 255) {}\n"
" if (u >= 255) {}\n"
" if (u <= 255) {}\n" // warn
" if (0 < u) {}\n"
" if (0 > u) {}\n" // warn
" if (0 <= u) {}\n" // warn
" if (0 >= u) {}\n"
" if (255 < u) {}\n" // warn
" if (255 > u) {}\n"
" if (255 <= u) {}\n"
" if (255 >= u) {}\n" // warn
"}\n", &settingsUnix64);
ASSERT_EQUALS("[test.cpp:3]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false.\n"
"[test.cpp:4]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true.\n"
"[test.cpp:6]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false.\n"
"[test.cpp:9]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always true.\n"
"[test.cpp:11]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always false.\n"
"[test.cpp:12]: (style) Comparing expression of type 'const unsigned char' against value 0. Condition is always true.\n"
"[test.cpp:14]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always false.\n"
"[test.cpp:17]: (style) Comparing expression of type 'const unsigned char' against value 255. Condition is always true.\n",
errout.str());
}

void knownConditionCast() { // #9976
Expand Down