Use Platform for char limits computation (#972)

This commit is contained in:
Dmitry-Me 2017-10-18 18:30:47 +03:00 committed by Daniel Marjamäki
parent b278436069
commit 151ace2581
3 changed files with 17 additions and 3 deletions

View File

@ -381,7 +381,9 @@ void CheckFunctions::memsetInvalid2ndParam()
if (printWarning && secondParamTok->isNumber()) { // Check if the second parameter is a literal and is out of range
const long long int value = MathLib::toLongNumber(secondParamTok->str());
if (value < -128 || value > 255) // FIXME: Use platform char_bits
const long long sCharMin = _settings->signedCharMin();
const long long uCharMax = _settings->unsignedCharMax();
if (value < sCharMin || value > uCharMax)
memsetValueOutOfRangeError(secondParamTok, secondParamTok->str());
}
}

View File

@ -131,6 +131,18 @@ namespace cppcheck {
return "unknown";
}
}
long long unsignedCharMax() const {
return max_value(char_bit + 1);
}
long long signedCharMax() const {
return max_value(char_bit);
}
long long signedCharMin() const {
return min_value(char_bit);
}
};
}

View File

@ -303,8 +303,8 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti
else if (valueType.type == ValueType::Type::LONGLONG)
setTokenValue(parent, castValue(value, valueType.sign, settings->long_long_bit), settings);
else if (value.isIntValue()) {
const int charMax = (1 << (settings->char_bit - 1)) - 1;
const int charMin = -charMax - 1;
const long long charMax = settings->signedCharMax();
const long long charMin = settings->signedCharMin();
if (charMin <= value.intvalue && value.intvalue <= charMax) {
// unknown type, but value is small so there should be no truncation etc
setTokenValue(parent,value,settings);