From 32604dd55ef617ec691fe2c9daf3c615a0f419de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 18 Jan 2010 21:34:11 +0100 Subject: [PATCH] Fixed #1266 ('qsort' missing in call_func_white_list) --- lib/checkmemoryleak.cpp | 13 +++++++++---- lib/checkmemoryleak.h | 3 +++ test/testmemleak.cpp | 28 +++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index af7f642e1..f9985f2a8 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -48,7 +48,7 @@ static const char * const call_func_white_list[] = , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" , "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy" , "memmove", "memset", "posix_fadvise", "posix_fallocate", "pread" - , "printf", "puts", "pwrite", "read", "readahead", "readdir", "readdir_r", "readv" + , "printf", "puts", "pwrite", "qsort", "read", "readahead", "readdir", "readdir_r", "readv" , "realloc", "return", "rewind", "rewinddir", "scandir", "seekdir" , "setbuf", "setbuffer", "setlinebuf", "setvbuf", "snprintf", "sprintf", "strcasecmp" , "strcat", "strchr", "strcmp", "strcpy", "stricmp", "strlen", "strncat", "strncmp" @@ -517,11 +517,16 @@ static int countParameters(const Token *tok) return -1; } +bool CheckMemoryLeakInFunction::test_white_list(const char funcname[]) +{ + return std::bsearch(funcname, call_func_white_list, + sizeof(call_func_white_list) / sizeof(call_func_white_list[0]), + sizeof(call_func_white_list[0]), call_func_white_list_compare); +} + const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list callstack, const unsigned int varid, AllocType &alloctype, AllocType &dealloctype, bool &all, unsigned int sz) { - if (std::bsearch(tok->strAt(0), call_func_white_list, - sizeof(call_func_white_list) / sizeof(call_func_white_list[0]), - sizeof(call_func_white_list[0]), call_func_white_list_compare)) + if (test_white_list(tok->strAt(0))) return 0; if (noreturn.find(tok->str()) != noreturn.end()) diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index ddd7bc903..fd21ee1f3 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -159,6 +159,9 @@ public: checkMemoryLeak.check(); } + /** For unit testing the white list */ + static bool test_white_list(const char funcname[]); + void check(); void localleaks(); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d199d0812..c7ca840e6 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -206,7 +206,8 @@ private: // Check that getcode works correctly.. TEST_CASE(testgetcode); - // Todo: check that call_func works correctly.. + // check that call_func works correctly.. + TEST_CASE(call_func); // Check that simplifycode works correctly.. TEST_CASE(simplifycode); @@ -505,6 +506,31 @@ private: } + void call_func() + { + // whitelist.. + ASSERT_EQUALS(true, CheckMemoryLeakInFunction::test_white_list("qsort")); + + static const char * const call_func_white_list[] = + { + "asprintf", "atof", "atoi", "atol", "clearerr", "delete", "fchmod", "fcntl" + , "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets" + , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "fscanf", "fseek" + , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" + , "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy" + , "memmove", "memset", "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", "strcasecmp" + , "strcat", "strchr", "strcmp", "strcpy", "stricmp", "strlen", "strncat", "strncmp" + , "strncpy", "strrchr", "strstr", "strtod", "strtol", "strtoul", "switch" + , "sync_file_range", "telldir", "typeid", "while", "write", "writev" + }; + + for (unsigned int i = 0; i < (sizeof(call_func_white_list) / sizeof(char *)); ++i) + ASSERT_EQUALS(true, CheckMemoryLeakInFunction::test_white_list(call_func_white_list[i])); + } + std::string simplifycode(const char code[], bool &all) const {