From 65f7bcbfa5c825b28b6432409c74569a5ee14ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 5 Aug 2010 08:19:36 +0200 Subject: [PATCH] null pointers: fixed TODO assertion - dereference pointer in function call and then checking that it is not NULL --- lib/checkother.cpp | 11 ++++++++--- test/testother.cpp | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f976887e2..95585fa97 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2454,17 +2454,22 @@ void CheckOther::nullPointerByDeRefAndChec() { if (Token::Match(tok1->tokAt(-2), "[=;{}] *")) { - nullPointerError(tok1, varname); + nullPointerError(tok1, varname, tok->linenr()); break; } - else if (tok1->previous() && tok1->previous()->str() == "&") + else if (Token::simpleMatch(tok1->previous(), "&")) { break; } - else if (tok1->next() && tok1->next()->str() == "=") + else if (Token::simpleMatch(tok1->next(), "=")) { break; } + // dereference in function call + else if (Token::Match(tok1->tokAt(-2), "[(,] *")) + { + nullPointerError(tok1, varname, tok->linenr()); + } } else if (tok1->str() == "{" || diff --git a/test/testother.cpp b/test/testother.cpp index cc8cb3069..6d638087f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -911,7 +911,7 @@ private: " if (!p)\n" " ;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); checkNullPointer("void foo(int *p)\n" "{\n" @@ -919,7 +919,7 @@ private: " if (!p)\n" " ;\n" "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 4\n", errout.str()); // no error checkNullPointer("void foo()\n"