Repository navigation
Fix #11592 FN compareValueOutOfTypeRangeError with char #8940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2009,14 +2009,15 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange() | |
| for (int i = 0; i < 2; ++i) { | ||
| const Token * const valueTok = (i == 0) ? tok->astOperand1() : tok->astOperand2(); | ||
| const Token * const typeTok = valueTok->astSibling(); | ||
| if (!valueTok->hasKnownIntValue() || !typeTok->valueType() || typeTok->valueType()->pointer) | ||
| const ValueType* const typeVt = typeTok->valueType(); | ||
| if (!valueTok->hasKnownIntValue() || !typeVt || typeVt->pointer) | ||
| continue; | ||
| if (valueTok->getKnownIntValue() < 0 && valueTok->valueType() && valueTok->valueType()->sign != ValueType::Sign::SIGNED) | ||
| continue; | ||
| if (typeTok->isLiteral()) | ||
| continue; | ||
| std::uint8_t bits = 0; | ||
| switch (typeTok->valueType()->type) { | ||
| switch (typeVt->type) { | ||
| case ValueType::Type::BOOL: | ||
| bits = 1; | ||
| break; | ||
|
|
@@ -2041,10 +2042,10 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange() | |
| if (bits == 0 || bits >= 63) | ||
| continue; | ||
|
|
||
| const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1))); | ||
| const auto typeMinValue = (typeVt->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1))); | ||
| const auto unsignedTypeMaxValue = (1LL << bits) - 1LL; | ||
| long long typeMaxValue; | ||
| if (typeTok->valueType()->sign != ValueType::Sign::SIGNED) | ||
| if (typeVt->sign != ValueType::Sign::SIGNED && !(typeVt->sign == ValueType::Sign::UNKNOWN_SIGN && typeVt->type == ValueType::Type::CHAR && mSettings.platform.defaultSign == 's')) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review Only lowercase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review Plain #define MARK ((char)0xE2)
int f(char c) {
if (c == (char)200) return 1; // "against value 200. Condition is always false"
if (c == static_cast<char>(0x80)) return 2; // "against value 128 ..."
if (c == MARK) return 3; // "against value 226 ..."
return 0;
}
|
||
| typeMaxValue = unsignedTypeMaxValue; | ||
| else if (bits >= mSettings.platform.int_bit && (!valueTok->valueType() || valueTok->valueType()->sign != ValueType::Sign::SIGNED)) | ||
| typeMaxValue = unsignedTypeMaxValue; | ||
|
|
@@ -2105,7 +2106,7 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange() | |
| } | ||
| if (!error || diag(tok)) | ||
| continue; | ||
| compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), kiv, result); | ||
| compareValueOutOfTypeRangeError(valueTok, typeVt->str(), kiv, result); | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AI review
Plain
charnow gets the signed range, but the operand conversions are not considered. WhenvalueTokis unsigned with a rank of at leastint(127u,sizeof(...)), a negativecbecomes a huge unsigned value, soc > 127uis true forc == -1. The PR still reports it as always false:--platform=unix64givesComparing expression of type 'char' against value 127. Condition is always false.and... against value 200 .... Base already has this false positive forsigned charandshort, but this change extends it to plainchar, wherec > sizeof(...)is common.==and!=against an unsigned value are still correct. The relational operators should be skipped, or should allow for wraparound, when the value operand is unsigned with a rank of at leastint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://trac.cppcheck.net/ticket/15111