From 4a7b60942b1deef4033a345d9c8ef8f911364ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 28 Mar 2011 17:28:21 +0200 Subject: [PATCH] Fixed #2691 (False positive: struct dereference and check (switch-break)) --- lib/checknullpointer.cpp | 4 ++-- test/testnullpointer.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 234bcd54a..b8025d72c 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -460,8 +460,8 @@ void CheckNullPointer::nullPointerStructByDeRefAndChec() else if (tok2->str() == "do") break; - // return at base level => stop checking - else if (indentlevel2 == 0 && tok2->str() == "return") + // return/break at base level => stop checking + else if (indentlevel2 == 0 && (tok2->str() == "return" || tok2->str() == "break")) break; // Function call: If the pointer is not a local variable it diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 25d99596f..619b40a2c 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -360,6 +360,19 @@ private: " if (abc) { }\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n",errout.str()); + + // #2691 - switch/break + check("void f(ABC *abc) {\n" + " switch ( x ) {\n" + " case 14:\n" + " sprintf(buf, \"%d\", abc->a);\n" + " break;\n" + " case 15:\n" + " if ( abc ) {}\n" + " break;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // Dereferencing a pointer and then checking if it is null