From fed875f4a4f3662623a84279738b8d9bc35470c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Zmys=C5=82owski?= Date: Mon, 7 Nov 2011 23:50:57 +0200 Subject: [PATCH] Fix #70 (Locate memory leaks when alloc happens in function parameter) http://sourceforge.net/apps/trac/cppcheck/ticket/70 --- lib/checkmemoryleak.cpp | 4 ++-- test/testmemleak.cpp | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 0a8f29e19..f7ab9f078 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -59,7 +59,7 @@ static const char * const call_func_white_list[] = { , "glob", "gmtime", "gmtime_r", "if", "index", "inet_addr", "inet_aton", "inet_network", "initgroups", "ioctl" , "link", "localtime", "localtime_r" , "lockf", "lseek", "lstat", "mblen", "mbstowcs", "mbtowc", "memchr", "memcmp", "memcpy", "memmove", "memset" - , "mkdir", "mkfifo", "mknod" + , "mkdir", "mkfifo", "mknod", "mkstemp" , "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf" , "perror", "popen" ,"posix_fadvise", "posix_fallocate", "pread" , "printf", "psignal", "putenv", "puts", "pwrite", "qsort", "read", "readahead", "readdir", "readdir_r" @@ -2889,7 +2889,7 @@ void CheckMemoryLeakNoVar::check() for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous()) { if (tok3->str() == "(") { // Is it a function call.. - if (Token::Match(tok3->tokAt(-2), "[(,;{}] %var% (")) { + if (Token::Match(tok3->tokAt(-2), "[(,;{}=] %var% (")) { const std::string functionName = tok3->strAt(-1); if (functionName == "delete" || functionName == "free" || diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d0d2bddaa..9da3b90e2 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -577,7 +577,7 @@ private: , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "freopen", "fscanf", "fseek" , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" , "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "open", "memchr", "memcpy" - , "memmove", "memset", "perror", "posix_fadvise", "posix_fallocate", "pread" + , "memmove", "memset", "mkstemp", "perror", "posix_fadvise", "posix_fallocate", "pread" , "printf", "puts", "pwrite", "read", "readahead", "readdir", "readdir_r", "readv" , "realloc", "return", "rewind", "rewinddir", "scandir", "seekdir" , "setbuf", "setbuffer", "setlinebuf", "setvbuf", "snprintf", "sprintf", "stpcpy", "strcasecmp" @@ -2217,6 +2217,7 @@ private: ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: f\n", errout.str()); } + void allocfunc1() { check("static char *a()\n" "{\n" @@ -5179,6 +5180,14 @@ private: " set_error(strdup(p));\n" "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Allocation with strdup, set_error doesn't release it.\n", errout.str()); + + check("void f()\n" + "{\n" + " int fd;\n" + " fd = mkstemp(strdup(\"/tmp/file.XXXXXXXX\"));\n" + " close(fd);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Allocation with strdup, mkstemp doesn't release it.\n", errout.str()); } }; static TestMemleakNoVar testMemleakNoVar;