From 8d0f315097d77d00abc108146131ac88ca0465e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 29 May 2012 21:10:19 +0200 Subject: [PATCH] Fixed #3851 (False positive memory leak (condition)) --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 5d75f549c..cab57af8f 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -292,7 +292,7 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, varInfo2.erase(tok->tokAt(2)->varId()); if (notzero.find(tok->tokAt(2)->varId()) != notzero.end()) varInfo2.clear(); - } else if (Token::Match(tok->next(), "( ! %var% )")) + } else if (Token::Match(tok->next(), "( ! %var% )|&&")) varInfo1.erase(tok->tokAt(3)->varId()); checkScope(tok2->next(), &varInfo1, notzero); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 6c5446a16..5e67dfbd5 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -62,6 +62,7 @@ private: TEST_CASE(ifelse2); TEST_CASE(ifelse3); TEST_CASE(ifelse4); + TEST_CASE(ifelse5); // switch TEST_CASE(switch1); @@ -313,6 +314,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void ifelse5() { + check("void f() {\n" + " char *p = malloc(10);\n" + " if (!p && x) { p = malloc(10); }\n" + " free(p);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void switch1() { check("void f() {\n" " char *p = 0;\n"