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))
|
||||
return;
|
||||
|
||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||
const std::size_t functions = symbolDatabase->functionScopes.size();
|
||||
for (std::size_t i = 0; i < functions; ++i) {
|
||||
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;
|
||||
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
||||
if (!tok->isArithmeticalOp() || Token::Match(tok,"+|-"))
|
||||
continue;
|
||||
|
||||
// Is result unsigned?
|
||||
if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED))
|
||||
continue;
|
||||
// Is result unsigned?
|
||||
if (!(tok->valueType() && tok->valueType()->sign == ValueType::Sign::UNSIGNED))
|
||||
continue;
|
||||
|
||||
// Check if an operand can be negative..
|
||||
std::stack<const Token *> tokens;
|
||||
tokens.push(tok->astOperand1());
|
||||
tokens.push(tok->astOperand2());
|
||||
while (!tokens.empty()) {
|
||||
const Token *tok1 = tokens.top();
|
||||
tokens.pop();
|
||||
if (!tok1)
|
||||
continue;
|
||||
if (!tok1->getValueLE(-1,_settings))
|
||||
continue;
|
||||
if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED)
|
||||
signConversionError(tok1, tok1->isNumber());
|
||||
}
|
||||
// Check if an operand can be negative..
|
||||
std::stack<const Token *> tokens;
|
||||
tokens.push(tok->astOperand1());
|
||||
tokens.push(tok->astOperand2());
|
||||
while (!tokens.empty()) {
|
||||
const Token *tok1 = tokens.top();
|
||||
tokens.pop();
|
||||
if (!tok1)
|
||||
continue;
|
||||
if (!tok1->getValueLE(-1,_settings))
|
||||
continue;
|
||||
if (tok1->valueType() && tok1->valueType()->sign != ValueType::Sign::UNSIGNED)
|
||||
signConversionError(tok1, tok1->isNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,6 +148,9 @@ private:
|
|||
}
|
||||
|
||||
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
|
||||
" return x * y;\n"
|
||||
"}\n"
|
||||
|
|
Loading…
Reference in New Issue