Clarify null pointer arithmetic message

This commit is contained in:
Daniel Marjamäki 2018-05-02 12:57:24 +02:00
parent 47ba7abf0b
commit 7dfbe7389b
2 changed files with 17 additions and 9 deletions

View File

@ -559,20 +559,28 @@ void CheckNullPointer::arithmetic()
void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value *value)
{
std::string arithmetic;
if (tok && tok->str()[0] == '-')
arithmetic = "subtraction";
else if (tok && tok->str()[0] == '+')
arithmetic = "addition";
else
arithmetic = "arithmetic";
std::string errmsg;
if (tok && tok->str().front() == '-') {
if (tok && tok->str()[0] == '-') {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is overflow in pointer subtraction.";
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is overflow in pointer " + arithmetic + ".";
else
errmsg = "Overflow in pointer arithmetic, NULL pointer is subtracted.";
} else {
if (value && value->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(value->condition) + " or there is pointer arithmetic with NULL pointer.";
else
errmsg = "Pointer arithmetic with NULL pointer.";
errmsg = "Pointer " + arithmetic + " with NULL pointer.";
}
const ErrorPath errorPath = getErrorPath(tok, value, tok && tok->str()[0] == '-' ? "Null pointer subtraction" : "Null pointer arithmetic");
const ErrorPath errorPath = getErrorPath(tok, value, "Null pointer " + arithmetic);
reportError(errorPath,
(value && value->condition) ? Severity::warning : Severity::error,

View File

@ -2612,7 +2612,7 @@ private:
" char * p = s + 20;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
@ -2624,7 +2624,7 @@ private:
" char * p = 20 + s;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
@ -2636,7 +2636,7 @@ private:
" s += 20;\n"
"}\n"
"void bar() { foo(0); }\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
ASSERT_EQUALS("[test.cpp:2]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("void foo(char *s) {\n"
" if (!s) {}\n"
@ -2645,10 +2645,10 @@ private:
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str());
check("int* f7() { int *x = NULL; return ++x; }");
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("int* f10() { int *x = NULL; return x++; } ");
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer arithmetic with NULL pointer.\n", errout.str());
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer addition with NULL pointer.\n", errout.str());
}
};