From f242cb639ef504a61deda415a35f6373c94bfd7c Mon Sep 17 00:00:00 2001 From: Ettl Martin Date: Mon, 21 Mar 2011 15:12:21 +0100 Subject: [PATCH] #2668 fixed memory leak ( freopen() ) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 50 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index cbc04fdeb..2474f34eb 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -48,7 +48,7 @@ static const char * const call_func_white_list[] = "access", "asctime", "asctime_r", "asprintf", "assert", "atof", "atoi", "atol", "chdir", "chmod", "chown" , "clearerr", "ctime", "ctime_r", "delete", "fchmod", "fclose", "fcntl" , "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets" - , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "fscanf", "fseek" + , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "freopen", "fscanf", "fseek" , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" , "fwrite", "getc", "gets", "gmtime", "gmtime_r", "if", "ioctl" , "localtime", "localtime_r" diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 8202ced06..af7028ce4 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -229,6 +229,7 @@ private: TEST_CASE(func19); // Ticket #2056 - if (!f(p)) return 0; TEST_CASE(func20); // Ticket #2182 - exit is not handled TEST_CASE(func21); // Ticket #2569 + TEST_CASE(func22); // Ticket #2668 TEST_CASE(allocfunc1); TEST_CASE(allocfunc2); @@ -567,7 +568,7 @@ private: { "access", "asprintf", "atof", "atoi", "atol", "chdir", "chmod", "clearerr", "chown", "delete" , "fchmod", "fcntl", "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets" - , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "fscanf", "fseek" + , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "freopen", "fscanf", "fseek" , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" , "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy" , "memmove", "memset", "posix_fadvise", "posix_fallocate", "pread" @@ -2005,6 +2006,53 @@ private: ASSERT_EQUALS("", errout.str()); } + // # 2668 + void func22() + { + check("void foo()\n" + "{\n" + " char * cpFile;\n" + " cpFile = new char [13];\n" + " strcpy (cpFile, \"testfile.txt\");\n" + " if(freopen(cpFile,\"w\",stdout)==0)\n" + " {\n" + " return;\n" + " }\n" + " delete [] cpFile;\n" + " fclose (stdout);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile\n", errout.str()); + + check("void foo()\n" + "{\n" + " char * cpFile;\n" + " cpFile = new char [13];\n" + " strcpy (cpFile, \"testfile.txt\");\n" + " if(freopen(cpFile,\"w\",stdout)==0)\n" + " {\n" + " delete [] cpFile;\n" + " return;\n" + " }\n" + " fclose (stdout);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: cpFile\n", errout.str()); + + check("void foo()\n" + "{\n" + " char * cpFile;\n" + " cpFile = new char [13];\n" + " strcpy (cpFile, \"testfile.txt\");\n" + " if(freopen(cpFile,\"w\",stdout)==0)\n" + " {\n" + " delete [] cpFile;\n" + " return;\n" + " }\n" + " delete [] cpFile;\n" + " fclose (stdout);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void allocfunc1() { check("static char *a()\n"