Integer Overflow: Don't warn about left-shift expressions with negative result like '1<<31'. That is common practice.
This commit is contained in:
parent
426d1b4196
commit
28af5c0379
|
@ -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,8 +136,14 @@ 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)
|
||||||
integerOverflowError(tok, *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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue