From 7dfbe7389b5998e66db676a273a93a3b3730732d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 2 May 2018 12:57:24 +0200 Subject: [PATCH] Clarify null pointer arithmetic message --- lib/checknullpointer.cpp | 16 ++++++++++++---- test/testnullpointer.cpp | 10 +++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index b5094feb0..19ee83679 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -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, diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 2929d0267..0982cc05e 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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()); } };