Integer Overflow: Don't warn about left-shift expressions with negative result like '1<<31'. That is common practice.

This commit is contained in:
Daniel Marjamäki 2016-12-21 18:19:59 +01:00
parent 426d1b4196
commit 28af5c0379
2 changed files with 16 additions and 5 deletions

View File

@ -113,7 +113,7 @@ void CheckType::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const Val
void CheckType::checkIntegerOverflow() void CheckType::checkIntegerOverflow()
{ {
// unknown sizeof(int) => can't run this checker // unknown sizeof(int) => can't run this checker
if (_settings->platformType == Settings::Unspecified) if (_settings->platformType == Settings::Unspecified || _settings->int_bit >= 64)
return; return;
// max int value according to platform settings. // max int value according to platform settings.
@ -136,7 +136,13 @@ void CheckType::checkIntegerOverflow()
const ValueFlow::Value *value = tok->getValueGE(maxint + 1, _settings); const ValueFlow::Value *value = tok->getValueGE(maxint + 1, _settings);
if (!value) if (!value)
value = tok->getValueLE(-maxint - 2, _settings); value = tok->getValueLE(-maxint - 2, _settings);
if (value) if (!value)
continue;
// For left shift, it's common practice to shift into the sign bit
if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (1LL << _settings->int_bit))
continue;
integerOverflowError(tok, *value); integerOverflowError(tok, *value);
} }
} }

View File

@ -42,7 +42,7 @@ private:
TEST_CASE(checkFloatToIntegerOverflow); TEST_CASE(checkFloatToIntegerOverflow);
} }
void check(const char code[], Settings* settings = 0) { void check(const char code[], Settings* settings = 0, const char filename[] = "test.cpp") {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
@ -55,7 +55,7 @@ private:
// Tokenize.. // Tokenize..
Tokenizer tokenizer(settings, this); Tokenizer tokenizer(settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, filename);
// Check.. // Check..
CheckType checkType(&tokenizer, settings, this); CheckType checkType(&tokenizer, settings, this);
@ -137,6 +137,11 @@ private:
" return 123456U * x;\n" " return 123456U * x;\n"
"}",&settings); "}",&settings);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("int foo() {\n"
" x = 1 << 31;\n" // this is technically integer overflow but it's common code
"}", &settings, "test.c");
ASSERT_EQUALS("", errout.str());
} }
void signConversion() { void signConversion() {