From cc89687e8ca30284b6eea6be098d1ee0120a38e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 31 Jul 2011 17:19:23 +0200 Subject: [PATCH] Null pointer: better handling when calling standard functions with possible null pointer --- lib/checknullpointer.cpp | 15 +++++++++++++++ test/testnullpointer.cpp | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 05d481523..1ea8ab909 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -715,6 +715,21 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() continue; } + // function call, check if pointer is dereferenced + if (Token::Match(tok2, "%var% (")) + { + std::list var; + parseFunctionCall(*tok2, var, 0); + for (std::list::const_iterator it = var.begin(); it != var.end(); ++it) + { + if ((*it)->varId() == varid) + { + nullPointerError(*it, pointerName, linenr); + break; + } + } + } + // calling unknown function (abort/init).. if (Token::simpleMatch(tok2, ") ;") && (Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") || diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f9f1b9009..93ddcd16c 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -985,6 +985,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); + check("void foo(char *p) {\n" + " if (p) {\n" + " }\n" + " strcpy(p, \"abc\");\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); + check("void foo(abc *p) {\n" " if (!p) {\n" " }\n"