Skip to content

Commit ad19835

Browse files
Fix #11592 FN compareValueOutOfTypeRangeError with char (#8940)
1 parent 8ffb76d commit ad19835

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎lib/checkcondition.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,14 +2009,15 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange()
20092009
for (int i = 0; i < 2; ++i) {
20102010
const Token * const valueTok = (i == 0) ? tok->astOperand1() : tok->astOperand2();
20112011
const Token * const typeTok = valueTok->astSibling();
2012-
if (!valueTok->hasKnownIntValue() || !typeTok->valueType() || typeTok->valueType()->pointer)
2012+
const ValueType* const typeVt = typeTok->valueType();
2013+
if (!valueTok->hasKnownIntValue() || !typeVt || typeVt->pointer)
20132014
continue;
20142015
if (valueTok->getKnownIntValue() < 0 && valueTok->valueType() && valueTok->valueType()->sign != ValueType::Sign::SIGNED)
20152016
continue;
20162017
if (typeTok->isLiteral())
20172018
continue;
20182019
std::uint8_t bits = 0;
2019-
switch (typeTok->valueType()->type) {
2020+
switch (typeVt->type) {
20202021
case ValueType::Type::BOOL:
20212022
bits = 1;
20222023
break;
@@ -2041,10 +2042,10 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange()
20412042
if (bits == 0 || bits >= 63)
20422043
continue;
20432044

2044-
const auto typeMinValue = (typeTok->valueType()->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1)));
2045+
const auto typeMinValue = (typeVt->sign == ValueType::Sign::UNSIGNED) ? 0 : (-(1LL << (bits-1)));
20452046
const auto unsignedTypeMaxValue = (1LL << bits) - 1LL;
20462047
long long typeMaxValue;
2047-
if (typeTok->valueType()->sign != ValueType::Sign::SIGNED)
2048+
if (typeVt->sign != ValueType::Sign::SIGNED && !(typeVt->sign == ValueType::Sign::UNKNOWN_SIGN && typeVt->type == ValueType::Type::CHAR && mSettings.platform.defaultSign == 's'))
20482049
typeMaxValue = unsignedTypeMaxValue;
20492050
else if (bits >= mSettings.platform.int_bit && (!valueTok->valueType() || valueTok->valueType()->sign != ValueType::Sign::SIGNED))
20502051
typeMaxValue = unsignedTypeMaxValue;
@@ -2105,7 +2106,7 @@ void CheckConditionImpl::checkCompareValueOutOfTypeRange()
21052106
}
21062107
if (!error || diag(tok))
21072108
continue;
2108-
compareValueOutOfTypeRangeError(valueTok, typeTok->valueType()->str(), kiv, result);
2109+
compareValueOutOfTypeRangeError(valueTok, typeVt->str(), kiv, result);
21092110
}
21102111
}
21112112
}

‎test/testcondition.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6652,6 +6652,12 @@ class TestCondition : public TestFixture {
66526652
"[test.cpp:2:28]: (style) Comparing expression of type 'signed int' against value 2147483647. Condition is always false. [compareValueOutOfTypeRangeError]\n"
66536653
"[test.cpp:7:17]: (style) Comparing expression of type 'signed int' against value 2147483647. Condition is always false. [compareValueOutOfTypeRangeError]\n",
66546654
errout_str());
6655+
6656+
check("void f(char c) {\n" // #11592
6657+
" if (c == 128) {}\n"
6658+
"}\n", settingsUnix64);
6659+
ASSERT_EQUALS("[test.cpp:2:14]: (style) Comparing expression of type 'char' against value 128. Condition is always false. [compareValueOutOfTypeRangeError]\n",
6660+
errout_str());
66556661
}
66566662

66576663
void knownConditionCast() {

0 commit comments

Comments
 (0)