checkIntegerOverflow: check all calculations, not only in function bodies
This commit is contained in:
parent
6bc0df2908
commit
cac7146cac
|
@ -145,32 +145,27 @@ void CheckType::checkIntegerOverflow()
|
||||||
// max int value according to platform settings.
|
// 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 - 1)) - 1;
|
||||||
|
|
||||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
||||||
const std::size_t functions = symbolDatabase->functionScopes.size();
|
if (!tok->isArithmeticalOp())
|
||||||
for (std::size_t i = 0; i < functions; ++i) {
|
continue;
|
||||||
const Scope * scope = symbolDatabase->functionScopes[i];
|
|
||||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
|
||||||
if (!tok->isArithmeticalOp())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// is result signed integer?
|
// is result signed integer?
|
||||||
const ValueType *vt = tok->valueType();
|
const ValueType *vt = tok->valueType();
|
||||||
if (!vt || vt->type != ValueType::Type::INT || vt->sign != ValueType::Sign::SIGNED)
|
if (!vt || vt->type != ValueType::Type::INT || vt->sign != ValueType::Sign::SIGNED)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// is there a overflow result value
|
// is there a overflow result value
|
||||||
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 || !_settings->isEnabled(value,false))
|
if (!value || !_settings->isEnabled(value,false))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// For left shift, it's common practice to shift into the sign bit
|
// 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))
|
if (tok->str() == "<<" && value->intvalue > 0 && value->intvalue < (1LL << _settings->int_bit))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
integerOverflowError(tok, *value);
|
integerOverflowError(tok, *value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,9 @@ private:
|
||||||
settings.platform(Settings::Unix32);
|
settings.platform(Settings::Unix32);
|
||||||
settings.addEnabled("warning");
|
settings.addEnabled("warning");
|
||||||
|
|
||||||
|
check("x = (int)0x10000 * (int)0x10000;", &settings);
|
||||||
|
ASSERT_EQUALS("[test.cpp:1]: (error) Signed integer overflow for expression '(int)65536*(int)65536'.\n", errout.str());
|
||||||
|
|
||||||
check("void foo() {\n"
|
check("void foo() {\n"
|
||||||
" int intmax = 0x7fffffff;\n"
|
" int intmax = 0x7fffffff;\n"
|
||||||
" return intmax + 1;\n"
|
" return intmax + 1;\n"
|
||||||
|
|
Loading…
Reference in New Issue