From 6901bcae7930f32b0e111d13eeef446eee7acc1a Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 5 Oct 2009 14:46:38 +0300 Subject: [PATCH] Fix #771 (False positive. Null pointer dereference in a switch case) http://sourceforge.net/apps/trac/cppcheck/ticket/771 --- src/checkother.cpp | 2 +- test/testother.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/checkother.cpp b/src/checkother.cpp index fde9847a1..401f386db 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -866,7 +866,7 @@ void CheckOther::nullPointer() // Check if the variable is dereferenced.. while (tok2) { - if (tok2->str() == "{" || tok2->str() == "}") + if (tok2->str() == "{" || tok2->str() == "}" || tok2->str() == "break") break; if (tok2->varId() == varid) diff --git a/test/testother.cpp b/test/testother.cpp index 457ea3728..e474ff365 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -628,6 +628,21 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + checkNullPointer("void foo(A*a)\n" + "{\n" + " switch (a->b()) {\n" + " case 1:\n" + " while( a ){\n" + " a = a->next;\n" + " }\n" + " break;\n" + " case 2:\n" + " a->b();\n" + " break;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void nullpointer2()