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()