diff --git a/lib/checktype.cpp b/lib/checktype.cpp index c57aa913b..1060fb2fa 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -354,7 +354,7 @@ void CheckType::checkFloatToIntegerOverflow() bits = _settings->long_long_bit; else continue; - if (bits < 64 && it->floatValue > (1ULL << (bits - 1))) + if (bits < 64 && it->floatValue >= (1ULL << bits)) floatToIntegerOverflowError(tok, *it); } } diff --git a/test/testtype.cpp b/test/testtype.cpp index d4e20db28..8f59ac036 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -238,6 +238,16 @@ private: " return (short)1E6;\n" "}\n"); ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () conversion overflow.\n", removeFloat(errout.str())); + + check("void f(void) {\n" + " return (unsigned char)256.0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (error) Undefined behaviour: float () conversion overflow.\n", removeFloat(errout.str())); + + check("void f(void) {\n" + " return (unsigned char)255.5;\n" + "}\n"); + ASSERT_EQUALS("", removeFloat(errout.str())); } };