From 9ccc57a4553c053893e1737044ad780a4fc2fa86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Nov 2010 08:35:23 +0100 Subject: [PATCH] Fixed #2214 (Improve check: Memory leak not detected when pointer is incremented) --- lib/checkmemoryleak.cpp | 18 ++++++++++++------ test/testmemleak.cpp | 3 +++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index bfbdaf5e4..561de989d 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -214,11 +214,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok if (Token::Match(tok, "delete [ ] ( %varid% ) ;", varid)) return NewArray; - if (Token::Match(tok, "free ( %varid% ) ;", varid) || - Token::Match(tok, "kfree ( %varid% ) ;", varid)) + if (Token::Match(tok, "free|kfree ( %varid% ) ;", varid) || + Token::Match(tok, "free|kfree ( %varid% -", varid)) return Malloc; - if (Token::Match(tok, "g_free ( %varid% ) ;", varid)) + if (Token::Match(tok, "g_free ( %varid% ) ;", varid) || + Token::Match(tok, "g_free ( %varid% -", varid)) return gMalloc; if (Token::Match(tok, "fclose ( %varid% )", varid) || @@ -1016,19 +1017,24 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext()) + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { if (tok2->str() == ";") + { + if (rhs) + tok = tok2; break; + } if (Token::Match(tok2, "[=+] %varid%", varid)) { rhs = true; - break; } } - addtoken(&rettail, tok, (rhs ? "use" : "assign")); + if (!rhs) + addtoken(&rettail, tok, "assign"); + continue; } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 4777f054e..e50988c63 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -534,6 +534,9 @@ private: ASSERT_EQUALS(";;;;", getcode("char *p; const char *q; q = p;", "p")); ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s")); + // non-use.. + ASSERT_EQUALS(";;", getcode("char *s; s = s + 1;", "s")); + // return.. ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s")); ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s"));