From 45821b38715d9354cd1c83c923488f56df73a870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 31 May 2010 18:20:34 +0200 Subject: [PATCH] Fixed #1719 (False negative: memory leak (fgets in condition)) --- lib/checkmemoryleak.cpp | 8 +++++++- test/testmemleak.cpp | 25 ++++--------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 25325a229..f2f4e1a80 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -963,7 +963,7 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list 0 && Token::Match(tok2, "%var% (")) + if (innerParlevel > 0 && Token::Match(tok2, "%var% (") && !test_white_list(tok2->str())) { bool use = false; for (const Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) @@ -1974,6 +1974,12 @@ const Token *CheckMemoryLeakInFunction::findleak(const Token *tokens) return result->tokAt(3); } + if (((result = Token::findmatch(tokens, "; alloc ; if dealloc ; }")) != NULL) && + !result->tokAt(7)) + { + return result->tokAt(6); + } + if ((result = Token::findmatch(tokens, "alloc ; }")) != NULL) { if (result->tokAt(3) == NULL) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 0ef24e783..2269f60ee 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -248,7 +248,6 @@ private: TEST_CASE(ifelse6); TEST_CASE(ifelse7); TEST_CASE(ifelse8); - TEST_CASE(ifelse9); TEST_CASE(ifelse10); TEST_CASE(if4); @@ -463,6 +462,7 @@ private: ASSERT_EQUALS(";;if(!var){}", getcode("char *s; if (a && !s) { }", "s")); ASSERT_EQUALS(";;ifv{}", getcode("char *s; if (foo(!s)) { }", "s")); ASSERT_EQUALS(";;;if{dealloc;};if{dealloc;return;}assign;returnuse;", getcode("char *buf, *tmp; tmp = realloc(buf, 40); if (!(tmp)) { free(buf); return; } buf = tmp; return buf;", "buf")); + ASSERT_EQUALS(";;if{}", getcode("FILE *f; if (fgets(buf,100,f)){}", "f")); // switch.. ASSERT_EQUALS(";;switch{case;;break;};", getcode("char *s; switch(a){case 1: break;};", "s")); @@ -770,6 +770,8 @@ private: ASSERT_EQUALS(notfound, dofindleak("alloc; if { return use; } dealloc;")); ASSERT_EQUALS(notfound, dofindleak("alloc; if { dealloc; return; } dealloc;")); + ASSERT_EQUALS(5, dofindleak("{\n;\n alloc;\n if dealloc;\n}")); + // assign.. ASSERT_EQUALS(2, dofindleak("alloc;\n assign;\n dealloc;")); ASSERT_EQUALS(notfound, dofindleak("alloc;\n if(!var) assign;\n dealloc;")); @@ -781,10 +783,6 @@ private: ASSERT_EQUALS(notfound, dofindleak("; loop { alloc ; if break; } dealloc ;")); ASSERT_EQUALS(1, dofindleak("; loop alloc ;")); ASSERT_EQUALS(1, dofindleak("; loop alloc ; dealloc ;")); - - // Todo.. - ASSERT_EQUALS(notfound, dofindleak("; alloc;\n if { dealloc; }\n ;")); - TODO_ASSERT_EQUALS(3, dofindleak("; alloc;\n if { dealloc; }\n ;")); } @@ -979,21 +977,6 @@ private: ASSERT_EQUALS("", errout.str()); } - - void ifelse9() - { - check("static char *f()\n" - "{\n" - " char *s = new char[10];\n" - " if ( ghfgf )\n" - " {\n" - " delete [] s;\n" - " }\n" - "}\n"); - ASSERT_EQUALS("", errout.str()); - } - - void ifelse10() { check("static char *f()\n" @@ -1235,7 +1218,7 @@ private: " };\n" "}\n"); check(code.c_str(), false); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: str\n", errout.str()); } void switch3()