From 92c5cffeb00e4b14c76c0fd82d7dcef796fa9b18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 May 2012 21:28:35 +0200 Subject: [PATCH] Memory leaks: Fixed false positive when allocated pointer is assigned --- lib/checkleakautovar.cpp | 3 ++- test/testleakautovar.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 2858ac561..5d75f549c 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -235,7 +235,8 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, continue; // Variable has already been allocated => error - leakIfAllocated(tok, *varInfo); + if (conditionalAlloc.find(tok->varId()) == conditionalAlloc.end()) + leakIfAllocated(tok, *varInfo); varInfo->erase(tok->varId()); // not a local variable nor argument? diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 52ab1483e..6c5446a16 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -42,6 +42,7 @@ private: TEST_CASE(assign7); TEST_CASE(assign8); TEST_CASE(assign9); + TEST_CASE(assign10); TEST_CASE(deallocuse1); TEST_CASE(deallocuse2); @@ -179,6 +180,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void assign10() { + check("void foo() {\n" + " char *p;\n" + " if (x) { p = malloc(10); }\n" + " if (!x) { p = NULL; }\n" + " free(p);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void deallocuse1() { check("void f(char *p) {\n" " free(p);\n"