From aac65247ba63ae30cffb91c6760b42ce677c28d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 20 Apr 2011 06:41:14 +0200 Subject: [PATCH] Fixed #2733 (Memory leak: not all branch are checked) --- lib/checkmemoryleak.cpp | 8 ++++++++ test/testmemleak.cpp | 33 +++++++-------------------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 23ee7c192..4886f6986 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1902,6 +1902,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) done = false; } + // Remove outer condition.. + else if (Token::Match(tok2->next(), "if { if return use ; }")) + { + tok2->tokAt(6)->deleteNext(); + Token::eraseTokens(tok2, tok2->tokAt(3)); + done = false; + } + continue; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 18bdcbfa1..015cad1be 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -232,7 +232,6 @@ private: TEST_CASE(func22); // Ticket #2668 TEST_CASE(func23); // Ticket #2667 TEST_CASE(func24); // Ticket #2705 - TEST_CASE(func25); // Ticket #2733 TEST_CASE(allocfunc1); TEST_CASE(allocfunc2); @@ -667,6 +666,13 @@ private: ASSERT_EQUALS("; dealloc ; return ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }")); + // remove outer if (#2733) + ASSERT_EQUALS("alloc ; return ; }", simplifycode("alloc ; if { if return use ; } return ; }")); + ASSERT_EQUALS("alloc ; return ; }", simplifycode("alloc ; if { if(var) return use ; } return ; }")); + TODO_ASSERT_EQUALS("alloc ; return ; }", + "alloc ; if(var) { if return use ; } return ; }", + simplifycode("alloc ; if(var) { if return use ; } return ; }")); + // "if ; .." ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;")); @@ -2200,31 +2206,6 @@ private: ASSERT_EQUALS("", errout.str()); } - // #2733 - void func25() - { - check("int* GetDeviceName(int a, int b)\n" - "{\n" - " int *p = new int[255];\n" - " memset(p, 0, 255 * sizeof(int));\n" - " if(a)\n" - " if(b)\n" - " return p;\n" - " return NULL; \n" - "}\n"); - TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n","", errout.str()); - - check("int* GetDeviceName(int a)\n" - "{\n" - " int *p = new int[255];\n" - " memset(p, 0, 255 * sizeof(int));\n" - " if(a)\n" - " return p;\n" - " return NULL; \n" - "}\n"); - ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str()); - } - void allocfunc1() { check("static char *a()\n"