Fix #70 (Locate memory leaks when alloc happens in function parameter)

http://sourceforge.net/apps/trac/cppcheck/ticket/70
This commit is contained in:
Marek Zmysłowski 2011-11-07 23:50:57 +02:00 committed by Reijo Tomperi
parent 7b7e072b88
commit fed875f4a4
2 changed files with 12 additions and 3 deletions

View File

@ -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" ||

View File

@ -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;