From 311f6dc92e0f51b5bb39a0e1212de37cab1b28a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 28 Sep 2009 22:58:06 +0200 Subject: [PATCH] Fixed #746 (False positive, Memory leak when goto is used) --- src/checkmemoryleak.cpp | 9 ++++++++- test/testmemleak.cpp | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 1b7e7a40d..94b43db5b 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -2116,10 +2116,17 @@ void CheckMemoryLeakStructMember::check() ignoredFunctions.insert("while"); ignoredFunctions.insert("malloc"); + + unsigned int indentlevel1 = 0; for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { + if (tok->str() == "{") + ++indentlevel1; + else if (tok->str() == "}") + --indentlevel1; + // Locate struct variables.. - if (Token::Match(tok, "struct|;|{|} %type% * %var% [=;]")) + if (indentlevel1 > 0 && Token::Match(tok, "struct|;|{|} %type% * %var% [=;]")) { const Token * const vartok = tok->tokAt(3); if (vartok->varId() == 0) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d0bf69c48..abc73318e 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2733,6 +2733,9 @@ private: // Handle if-else TEST_CASE(ifelse); + + // struct variable is a global variable + TEST_CASE(globalvar); } void err() @@ -2896,6 +2899,19 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); } + + void globalvar() + { + check("struct ABC *abc;\n" + "\n" + "static void foo()\n" + "{\n" + " abc = malloc(sizeof(struct ABC));\n" + " abc->a = malloc(10);\n" + " return;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } };