signed int overflow: warn about intmax+1

This commit is contained in:
Daniel Marjamäki 2016-11-05 09:29:52 +01:00
parent 2885a75ea6
commit f280061ff8
2 changed files with 10 additions and 4 deletions

View File

@ -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();

View File

@ -88,10 +88,10 @@ private:
// Ticket #6793
check("template<int I> 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 I> 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"