From 5b81c92a149a66beacd171ed88a38a54bed8f1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 23 Jul 2009 19:59:29 +0200 Subject: [PATCH] null pointers: fixed false positives when checking if pointer is null at many locations (#485) --- src/checkother.cpp | 15 +++++++++++++++ test/testother.cpp | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index 9c90b8008..44719e0dd 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1042,6 +1042,21 @@ void CheckOther::nullPointer() if (varid1 == 0) continue; + // Checking if the struct pointer is non-null before the assignment.. + { + const Token *tok2 = _tokenizer->tokens(); + while (tok2) + { + if (tok2 == tok1) + break; + if (Token::Match(tok2, "if|while ( !| %varid% )", varid1)) + break; + tok2 = tok2->next(); + } + if (tok2 != tok1) + continue; + } + unsigned int indentlevel2 = 0; for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next()) { diff --git a/test/testother.cpp b/test/testother.cpp index cd6a59973..0280b0de8 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -508,6 +508,18 @@ private: " ;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // while.. + checkNullPointer("void freeAbc(struct ABC *abc)\n" + "{\n" + " while (abc)\n" + " {\n" + " struct ABC *next = abc->next;\n" + " if (abc) delete abc;\n" + " abc = next;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // Dereferencing a pointer and then checking if it is null