From 421a8da6a8300aa362ea1db0d3d6d07eb183d698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 17 Jul 2019 22:41:24 +0200 Subject: [PATCH] Try to clarify signConversion message --- lib/checktype.cpp | 4 ++-- test/testtype.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 3421dc286..1d35c56b8 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -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); } diff --git a/test/testtype.cpp b/test/testtype.cpp index e8eb3badf..7beef12ac 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -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() {