Fixed #6168 (False positive: sign conversion for inner calculation)

This commit is contained in:
Daniel Marjamäki 2014-09-14 10:29:58 +02:00
parent 865a252c89
commit d354cdc02c
2 changed files with 8 additions and 3 deletions

View File

@ -238,7 +238,8 @@ void CheckType::checkSignConversion()
// Check if there are signed operands that can be negative..
std::stack<const Token *> tokens;
tokens.push(tok);
tokens.push(tok->astOperand1());
tokens.push(tok->astOperand2());
while (!tokens.empty()) {
const Token *tok1 = tokens.top();
tokens.pop();
@ -268,8 +269,6 @@ void CheckType::checkSignConversion()
break;
}
}
tokens.push(tok1->astOperand1());
tokens.push(tok1->astOperand2());
}
}
}

View File

@ -117,6 +117,12 @@ private:
"void f2() { f1(-4); }");
ASSERT_EQUALS("[test.cpp:1]: (warning) Suspicious code: sign conversion of x in calculation, even though x can have a negative value\n", errout.str());
check("unsigned int f1(int x) {" // #6168: FP for inner calculation
" return 5U * (1234 - x);\n" // <- signed subtraction, x is not sign converted
"}\n"
"void f2() { f1(-4); }");
ASSERT_EQUALS("", errout.str());
// Dont warn for + and -
check("void f1(int x) {"
" a = x + 5U;\n"