diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8a9f0edf730..c43ac014116 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2495,7 +2495,7 @@ void CheckOther::nullPointerByDeRefAndChec() // Check that variable is a pointer.. const Token *decltok = Token::findmatch(_tokenizer->tokens(), "%varid%", varid); - if (!Token::Match(decltok->tokAt(-3), "[;,(] %type% *")) + if (!Token::Match(decltok->tokAt(-3), "[{};,(] %type% *")) continue; for (const Token *tok1 = tok->previous(); tok1 && tok1 != decltok; tok1 = tok1->previous()) @@ -2516,7 +2516,7 @@ void CheckOther::nullPointerByDeRefAndChec() break; } // dereference in function call - else if (Token::Match(tok1->tokAt(-2), "[(,] *")) + else if (Token::Match(tok1->tokAt(-3), "!!sizeof [(,] *")) { nullPointerError(tok1, varname, tok->linenr()); } diff --git a/test/testother.cpp b/test/testother.cpp index 192c5de44e4..cb89c5ff5c0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -695,6 +695,17 @@ class TestOther : public TestFixture " if (a) { }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // ticket #2134 - sizeof doesn't dereference + checkNullPointer("void f() {\n" + " int c = 1;\n" + " int *list = NULL;\n" + " sizeof(*list);\n" + " if (!list)\n" + " ;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } void nullpointer2()