From 0ead18122dd751207b36434520b6b9612f969914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 27 Jul 2012 12:25:20 +0200 Subject: [PATCH] Fixed #3695 (False positive: memory leak (ptr?free(ptr):0)) --- lib/checkmemoryleak.cpp | 5 ++++- test/testmemleak.cpp | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 678864bfb..7c3b8b39c 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -269,7 +269,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok if (tok && tok->str() == "::") tok = tok->next(); - if (Token::Match(tok, "free|kfree ( %varid% ) ;", varid) || + if (Token::Match(tok, "free|kfree ( %varid% ) [;:]", varid) || Token::Match(tok, "free|kfree ( %varid% -", varid) || Token::Match(tok, "realloc ( %varid% , 0 ) ;", varid)) return Malloc; @@ -989,6 +989,9 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listprevious(), "[;{})=|] ::| %var%")) { + if (Token::Match(tok, "%varid% ?", varid)) + tok = tok->tokAt(2); + AllocType dealloc = getDeallocationType(tok, varid); if (dealloc != No && tok->str() == "fcloseall" && alloctype != dealloc) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 8178ccb49..0bd5bdd32 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -436,6 +436,7 @@ private: ASSERT_EQUALS(";;dealloc;", getcode("void *p; foo(close(p));", "p")); ASSERT_EQUALS(";;;;", getcode("FILE *f1; FILE *f2; fclose(f1);", "f2")); ASSERT_EQUALS(";;returnuse;", getcode("FILE *f; return fclose(f) == EOF ? 1 : 2;", "f")); + ASSERT_EQUALS(";;dealloc;", getcode("char *s; s ? free(s) : 0;", "s")); // if.. ASSERT_EQUALS(";;if{}", getcode("char *s; if (a) { }", "s"));