checkSignConversion: check all calculations, not only in function bodies
This commit is contained in:
parent
f6e30eee19
commit
9268c2034a
|
@ -210,32 +210,27 @@ void CheckType::checkSignConversion()
|
||||||
if (!_settings->isEnabled(Settings::WARNING))
|
if (!_settings->isEnabled(Settings::WARNING))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
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() || Token::Match(tok,"+|-"))
|
||||||
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() || Token::Match(tok,"+|-"))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Is result unsigned?
|
// Is result unsigned?
|
||||||
if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED))
|
if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check if an operand can be negative..
|
// Check if an operand can be negative..
|
||||||
std::stack<const Token *> tokens;
|
std::stack<const Token *> tokens;
|
||||||
tokens.push(tok->astOperand1());
|
tokens.push(tok->astOperand1());
|
||||||
tokens.push(tok->astOperand2());
|
tokens.push(tok->astOperand2());
|
||||||
while (!tokens.empty()) {
|
while (!tokens.empty()) {
|
||||||
const Token *tok1 = tokens.top();
|
const Token *tok1 = tokens.top();
|
||||||
tokens.pop();
|
tokens.pop();
|
||||||
if (!tok1)
|
if (!tok1)
|
||||||
continue;
|
continue;
|
||||||
if (!tok1->getValueLE(-1,_settings))
|
if (!tok1->getValueLE(-1,_settings))
|
||||||
continue;
|
continue;
|
||||||
if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED)
|
if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED)
|
||||||
signConversionError(tok1, tok1->isNumber());
|
signConversionError(tok1, tok1->isNumber());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,6 +148,9 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void signConversion() {
|
void signConversion() {
|
||||||
|
check("x = -4 * (unsigned)y;");
|
||||||
|
ASSERT_EQUALS("[test.cpp:1]: (warning) Suspicious code: sign conversion of -4 in calculation because '-4' has a negative value\n", errout.str());
|
||||||
|
|
||||||
check("unsigned int f1(signed int x, unsigned int y) {" // x is signed
|
check("unsigned int f1(signed int x, unsigned int y) {" // x is signed
|
||||||
" return x * y;\n"
|
" return x * y;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
|
|
Loading…
Reference in New Issue