Try to clarify signConversion message

This commit is contained in:
Daniel Marjamäki 2019-07-17 22:41:24 +02:00
parent f0aeb845e5
commit 421a8da6a8
2 changed files with 8 additions and 8 deletions

View File

@ -250,9 +250,9 @@ void CheckType::signConversionError(const Token *tok, const bool constvalue)
if (tok && tok->isName())
msg << "$symbol:" << expr << "\n";
if (constvalue)
msg << "Suspicious code: sign conversion of '" << expr << "' in calculation because '" << expr << "' has a negative value";
msg << "Expression '" << expr << "' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.";
else
msg << "Suspicious code: sign conversion of '" << expr << "' in calculation, even though '" << expr << "' can have a negative value";
msg << "Expression '" << expr << "' can have a negative value. That is converted to an unsigned value and used in an unsigned calculation.";
reportError(tok, Severity::warning, "signConversion", msg.str(), CWE195, false);
}

View File

@ -188,25 +188,25 @@ 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());
ASSERT_EQUALS("[test.cpp:1]: (warning) Expression '-4' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
check("unsigned int dostuff(int x) {\n" // x is signed
" if (x==0) {}\n"
" return (x-1)*sizeof(int);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Suspicious code: sign conversion of 'x-1' in calculation, even though 'x-1' can have a negative value\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (warning) Expression 'x-1' can have a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
check("unsigned int f1(signed int x, unsigned int y) {" // x is signed
" return x * y;\n"
"}\n"
"void f2() { f1(-4,4); }");
TODO_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());
TODO_ASSERT_EQUALS("error", "", errout.str());
check("unsigned int f1(int x) {" // x has no signedness, but it can have the value -1 so assume it's signed
check("unsigned int f1(int x) {"
" return x * 5U;\n"
"}\n"
"void f2() { f1(-4); }");
TODO_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());
TODO_ASSERT_EQUALS("error", "", 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
@ -224,7 +224,7 @@ private:
check("size_t foo(size_t x) {\n"
" return -2 * x;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) Suspicious code: sign conversion of '-2' in calculation because '-2' has a negative value\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (warning) Expression '-2' has a negative value. That is converted to an unsigned value and used in an unsigned calculation.\n", errout.str());
}
void longCastAssign() {