From b2343a2d4b76a3d7c29b265390fc01db6b48ecd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 1 May 2018 17:30:29 +0200 Subject: [PATCH] Fixed #8518 (Clarify warning for a NULL pointer which is received by a function call parameter.) --- lib/checknullpointer.cpp | 7 ++----- test/testnullpointer.cpp | 10 +++++----- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 986820f2e..b5094feb0 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -572,12 +572,9 @@ void CheckNullPointer::arithmeticError(const Token *tok, const ValueFlow::Value errmsg = "Pointer arithmetic with NULL pointer."; } - std::list callstack; - callstack.push_back(tok); - if (value && value->condition) - callstack.push_back(value->condition); + const ErrorPath errorPath = getErrorPath(tok, value, tok && tok->str()[0] == '-' ? "Null pointer subtraction" : "Null pointer arithmetic"); - reportError(callstack, + reportError(errorPath, (value && value->condition) ? Severity::warning : Severity::error, (value && value->condition) ? "nullPointerArithmeticRedundantCheck" : "nullPointerArithmetic", errmsg, diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 22519bfca..2929d0267 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -2586,7 +2586,7 @@ private: " if (!s) {}\n" " p = s - 20;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str()); check("void foo(char *s) {\n" " s -= 20;\n" @@ -2598,7 +2598,7 @@ private: " if (!s) {}\n" " s -= 20;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction.\n", errout.str()); check("int* f8() { int *x = NULL; return --x; }"); ASSERT_EQUALS("[test.cpp:1]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted.\n", errout.str()); @@ -2618,7 +2618,7 @@ private: " if (!s) {}\n" " char * p = s + 20;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str()); + 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("void foo(char *s) {\n" " char * p = 20 + s;\n" @@ -2630,7 +2630,7 @@ private: " if (!s) {}\n" " char * p = 20 + s;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str()); + 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("void foo(char *s) {\n" " s += 20;\n" @@ -2642,7 +2642,7 @@ private: " if (!s) {}\n" " s += 20;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer.\n", errout.str()); + 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());