From cdb685c83c83db1e778598867a9a65a9dbe3791a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 29 Nov 2010 20:19:31 +0100 Subject: [PATCH] Fixed #2245 (False positive: Possible null pointer dereference) --- lib/checknullpointer.cpp | 9 ++++++++- test/testnullpointer.cpp | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 6554946ae..86672e7d5 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -490,6 +490,13 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() break; } + // parameters to sizeof are not dereferenced + if (Token::Match(tok2, "decltype|sizeof (")) + { + tok2 = tok2->next()->link(); + continue; + } + // abort function.. if (Token::simpleMatch(tok2, ") ; }") && Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (")) @@ -550,7 +557,7 @@ void CheckNullPointer::nullConstantDereference() --indentlevel; } - if (tok->str() == "(" && Token::simpleMatch(tok->previous(), "sizeof")) + if (tok->str() == "(" && Token::Match(tok->previous(), "sizeof|decltype")) tok = tok->link(); else if (Token::simpleMatch(tok, "exit ( )")) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 94e4f2b2f..38fa6db5e 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -154,6 +154,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + // ticket #2245 - sizeof doesn't dereference + check("void f(Bar *p) {\n" + " if (!p) {\n" + " int sz = sizeof(p->x);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } void nullpointer2()