diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 447dcd032..d67cc02ed 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1223,7 +1223,7 @@ template static T getvalue(const int test, const T value1, const T valu case 2: return value1; case 3: - return (value1 + value2) / (T)2; + return (T)(value1*0.5 + value2*0.5); case 4: return value2; case 5: { diff --git a/test/testother.cpp b/test/testother.cpp index abe04cde4..2294a91b9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -196,6 +196,8 @@ private: TEST_CASE(checkCommaSeparatedReturn); TEST_CASE(checkComparisonFunctionIsAlwaysTrueOrFalse); + + TEST_CASE(integerOverflow) // #5895 } void check(const char code[], const char *filename = nullptr, bool experimental = false, bool inconclusive = true, bool posix = false, bool runSimpleChecks=true, Settings* settings = 0) { @@ -7246,6 +7248,16 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void integerOverflow() { // 5895 + // no signed integer overflow should happen + check("#define A 0x89504e470d0a1a0a\n" + "#define B 0x8a4d4e470d0a1a0a\n" + "void f(unsigned long long ull) {\n" + " if (ull == A || ull == B);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestOther)