From 9370f552ac0434f39c90ae08a502d3c6175b7076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 26 Oct 2010 18:26:02 +0200 Subject: [PATCH] Fixed #2134 (sizeof(*list) complains of null pointer dereference) --- lib/checkother.cpp | 4 ++-- test/testother.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8a9f0edf7..c43ac0141 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 192c5de44..cb89c5ff5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -695,6 +695,17 @@ private: " 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()