From d3d9c76f019feccdf2ed5fe8183e21a20a9b28a6 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Mon, 14 Aug 2017 18:07:36 +0300 Subject: [PATCH] Fix off by one, resolve C4334 --- lib/valueflow.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a5d1b4416..6daa4ad75 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -290,9 +290,14 @@ static void setTokenValue(Token* tok, const ValueFlow::Value &value, const Setti setTokenValue(parent, castValue(value, valueType.sign, settings->long_bit), settings); else if (valueType.type == ValueType::Type::LONGLONG) setTokenValue(parent, castValue(value, valueType.sign, settings->long_long_bit), settings); - else if (value.isIntValue() && value.intvalue < (1 << (settings->char_bit - 1)) && value.intvalue > -(1 << (settings->char_bit - 1))) - // unknown type, but value is small so there should be no truncation etc - setTokenValue(parent,value,settings); + else if (value.isIntValue()) { + const int charMax = (1 << (settings->char_bit - 1)) - 1; + const int charMin = -charMax - 1; + if (charMin <= value.intvalue && value.intvalue <= charMax) { + // unknown type, but value is small so there should be no truncation etc + setTokenValue(parent,value,settings); + } + } } else if (parent->str() == ":") {