From 151ace258185456b72e7cc825c951d20c1247ce3 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 18 Oct 2017 18:30:47 +0300 Subject: [PATCH] Use Platform for char limits computation (#972) --- lib/checkfunctions.cpp | 4 +++- lib/platform.h | 12 ++++++++++++ lib/valueflow.cpp | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 6135a6625..3b345d025 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -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()); } } diff --git a/lib/platform.h b/lib/platform.h index 2824d1b7e..3e316de8a 100644 --- a/lib/platform.h +++ b/lib/platform.h @@ -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); + } }; } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 3cb9c058d..23b02df42 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -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);