diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 01c371586..956b3b434 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -117,7 +117,7 @@ void CheckType::checkIntegerOverflow() return; // max int value according to platform settings. - const MathLib::bigint maxint = (1LL << (_settings->int_bit - 1)) - 1; + const MathLib::bigint maxint = (1LL << (_settings->int_bit - 2)) - 1; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); const std::size_t functions = symbolDatabase->functionScopes.size(); diff --git a/test/testtype.cpp b/test/testtype.cpp index 13c37917c..ec2bbdcb9 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -88,10 +88,10 @@ private: // Ticket #6793 check("template int foo(int x) { return x << I; }\n" - "const int f = foo<31>(1);\n" - "const int g = foo<100>(1);\n" + "const int f = foo<31>(0);\n" + "const int g = foo<100>(0);\n" "template int hoo(int x) { return x << 32; }\n" - "const int h = hoo<100>(1);", &settings); + "const int h = hoo<100>(0);", &settings); ASSERT_EQUALS("[test.cpp:4]: (error) Shifting 32-bit value by 32 bits is undefined behaviour\n" "[test.cpp:1]: (error) Shifting 32-bit value by 100 bits is undefined behaviour\n", errout.str()); @@ -107,6 +107,12 @@ private: settings.platform(Settings::Unix32); settings.addEnabled("warning"); + check("void foo() {\n" + " int intmax = 0x7fffffff;\n" + " return intmax + 1;\n" + "}",&settings); + ASSERT_EQUALS("[test.cpp:3]: (error) Signed integer overflow for expression 'intmax+1'.\n", errout.str()); + check("int foo(signed int x) {\n" " if (x==123456) {}\n" " return x * x;\n"