diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index d31e2b9e9..4acea2df7 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -61,34 +61,33 @@ static unsigned int countParameters(const Token *tok) * This list contains function names with const parameters e.g.: atof(const char *) * Reference: http://www.aquaphoenix.com/ref/gnu_c_library/libc_492.html#SEC492 */ -static const char * const call_func_white_list[] = { - "_open", "_wopen", "access", "adjtime", "asctime", "asctime_r", "asprintf", "assert" - , "atof", "atoi", "atol", "chdir", "chmod", "chown" - , "clearerr", "creat", "ctime", "ctime_r", "execl", "execle" - , "execlp", "execv", "execve", "fchmod", "fclose", "fcntl" - , "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets" - , "flock", "fmemopen", "fnmatch", "fopen", "fopencookie", "for", "fprintf", "fputc", "fputs", "fread", "free" - , "freopen", "fscanf", "fseek", "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello" - , "ftruncate", "fwrite", "getc", "getenv","getgrnam", "gethostbyaddr", "gethostbyname", "getnetbyname" - , "getopt", "getopt_long", "getprotobyname", "getpwnam", "gets", "getservbyname", "getservbyport" - , "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", "mkstemp" - , "obstack_printf", "obstack_vprintf", "open", "opendir", "parse_printf_format", "pathconf" - , "perror", "popen" ,"posix_fadvise", "posix_fallocate", "pread" - , "printf", "psignal", "puts", "pwrite", "qsort", "read", "readahead", "readdir", "readdir_r" - , "readlink", "readv" - , "realloc", "regcomp", "remove", "rename", "return", "rewind", "rewinddir", "rindex" - , "rmdir" ,"scandir", "scanf", "seekdir" - , "setbuf", "setbuffer", "sethostname", "setlinebuf", "setlocale" ,"setvbuf", "sizeof" ,"snprintf", "sprintf", "sscanf" - , "stat", "stpcpy", "strcasecmp", "strcat", "strchr", "strcmp", "strcoll" - , "strcpy", "strcspn", "strdup", "stricmp", "strlen", "strncasecmp", "strncat", "strncmp" - , "strncpy", "strpbrk","strrchr", "strspn", "strstr", "strtod", "strtok", "strtol", "strtoul", "strxfrm", "switch" - , "symlink", "sync_file_range", "system", "telldir", "tempnam", "time", "typeid", "unlink" - , "utime", "utimes", "vasprintf", "vfprintf", "vfscanf", "vprintf" - , "vscanf", "vsnprintf", "vsprintf", "vsscanf", "while", "wordexp","write", "writev" -}; +static const std::set call_func_white_list = make_container < std::set > () + << "_open" << "_wopen" << "access" << "adjtime" << "asctime" << "asctime_r" << "asprintf" << "assert" + << "atof" << "atoi" << "atol" << "chdir" << "chmod" << "chown" + << "clearerr" << "creat" << "ctime" << "ctime_r" << "execl" << "execle" + << "execlp" << "execv" << "execve" << "fchmod" << "fclose" << "fcntl" + << "fdatasync" << "feof" << "ferror" << "fflush" << "fgetc" << "fgetpos" << "fgets" + << "flock" << "fmemopen" << "fnmatch" << "fopen" << "fopencookie" << "for" << "fprintf" << "fputc" << "fputs" << "fread" << "free" + << "freopen" << "fscanf" << "fseek" << "fseeko" << "fsetpos" << "fstat" << "fsync" << "ftell" << "ftello" + << "ftruncate" << "fwrite" << "getc" << "getenv" << "getgrnam" << "gethostbyaddr" << "gethostbyname" << "getnetbyname" + << "getopt" << "getopt_long" << "getprotobyname" << "getpwnam" << "gets" << "getservbyname" << "getservbyport" + << "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" << "mkstemp" + << "obstack_printf" << "obstack_vprintf" << "open" << "opendir" << "parse_printf_format" << "pathconf" + << "perror" << "popen" << "posix_fadvise" << "posix_fallocate" << "pread" + << "printf" << "psignal" << "puts" << "pwrite" << "qsort" << "read" << "readahead" << "readdir" << "readdir_r" + << "readlink" << "readv" + << "realloc" << "regcomp" << "remove" << "rename" << "return" << "rewind" << "rewinddir" << "rindex" + << "rmdir" << "scandir" << "scanf" << "seekdir" + << "setbuf" << "setbuffer" << "sethostname" << "setlinebuf" << "setlocale" << "setvbuf" << "sizeof" << "snprintf" << "sprintf" << "sscanf" + << "stat" << "stpcpy" << "strcasecmp" << "strcat" << "strchr" << "strcmp" << "strcoll" + << "strcpy" << "strcspn" << "strdup" << "stricmp" << "strlen" << "strncasecmp" << "strncat" << "strncmp" + << "strncpy" << "strpbrk" << "strrchr" << "strspn" << "strstr" << "strtod" << "strtok" << "strtol" << "strtoul" << "strxfrm" << "switch" + << "symlink" << "sync_file_range" << "system" << "telldir" << "tempnam" << "time" << "typeid" << "unlink" + << "utime" << "utimes" << "vasprintf" << "vfprintf" << "vfscanf" << "vprintf" + << "vscanf" << "vsnprintf" << "vsprintf" << "vsscanf" << "while" << "wordexp" << "write" << "writev"; //--------------------------------------------------------------------------- @@ -529,9 +528,7 @@ bool CheckMemoryLeakInFunction::notvar(const Token *tok, unsigned int varid, boo bool CheckMemoryLeakInFunction::test_white_list(const std::string &funcname, const Settings *settings, bool cpp) { - return (std::binary_search(call_func_white_list, - call_func_white_list+sizeof(call_func_white_list) / sizeof(call_func_white_list[0]), - funcname) || (settings->library.leakignore.find(funcname) != settings->library.leakignore.end()) || (cpp && funcname == "delete")); + return ((call_func_white_list.find(funcname)!=call_func_white_list.end()) || (settings->library.leakignore.find(funcname) != settings->library.leakignore.end()) || (cpp && funcname == "delete")); } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 88b92025e..9e361f8a7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -466,7 +466,7 @@ void CheckOther::invalidPointerCast() const Variable *var = nullptr; bool allocation = false; bool ref = false; - if (Token::Match(nextTok, "new %type%")) + if (_tokenizer->isCPP() && Token::Match(nextTok, "new %type%")) allocation = true; else if (Token::Match(nextTok, "%var% !![")) var = nextTok->variable(); diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index a93669ed9..844aa68a9 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -227,27 +227,20 @@ void CheckStl::mismatchingContainersError(const Token *tok) } namespace { - static const char* const algorithm2_strings[] = { // func(begin1, end1 - "adjacent_find", "all_of", "any_of", "binary_search", "copy", "copy_if", "count", "count_if", "equal", "equal_range", - "find", "find_if", "find_if_not", "for_each", "generate", "is_heap", "is_heap_until", "is_partitioned", - "is_permutation", "is_sorted", "is_sorted_until", "lower_bound", "make_heap", "max_element", "minmax_element", - "min_element", "mismatch", "move", "move_backward", "next_permutation", "none_of", "partition", "partition_copy", - "partition_point", "pop_heap", "prev_permutation", "push_heap", "random_shuffle", "remove", "remove_copy", - "remove_copy_if", "remove_if", "replace", "replace_copy", "replace_copy_if", "replace_if", "reverse", "reverse_copy", - "search_n", "shuffle", "sort", "sort_heap", "stable_partition", "stable_sort", "swap_ranges", "transform", "unique", - "unique_copy", "upper_bound" - }; - static const char* const algorithm22_strings[] = { // func(begin1, end1, begin2, end2 - "find_end", "find_first_of", "includes", "lexicographical_compare", "merge", "partial_sort_copy", - "search", "set_difference", "set_intersection", "set_symmetric_difference", "set_union" - }; - static const char* const algorithm1x1_strings[] = { // func(begin1, x, end1 - "inplace_merge", "nth_element", "partial_sort", "rotate", "rotate_copy" - }; - - static const std::set algorithm2(algorithm2_strings, &algorithm2_strings[sizeof(algorithm2_strings) / sizeof(*algorithm2_strings)]); - static const std::set algorithm22(algorithm22_strings, &algorithm22_strings[sizeof(algorithm22_strings) / sizeof(*algorithm22_strings)]); - static const std::set algorithm1x1(algorithm1x1_strings, &algorithm1x1_strings[sizeof(algorithm1x1_strings) / sizeof(*algorithm1x1_strings)]); + const std::set algorithm2 = make_container< std::set >() // func(begin1, end1 + << "adjacent_find" << "all_of" << "any_of" << "binary_search" << "copy" << "copy_if" << "count" << "count_if" << "equal" << "equal_range" + << "find" << "find_if" << "find_if_not" << "for_each" << "generate" << "is_heap" << "is_heap_until" << "is_partitioned" + << "is_permutation" << "is_sorted" << "is_sorted_until" << "lower_bound" << "make_heap" << "max_element" << "minmax_element" + << "min_element" << "mismatch" << "move" << "move_backward" << "next_permutation" << "none_of" << "partition" << "partition_copy" + << "partition_point" << "pop_heap" << "prev_permutation" << "push_heap" << "random_shuffle" << "remove" << "remove_copy" + << "remove_copy_if" << "remove_if" << "replace" << "replace_copy" << "replace_copy_if" << "replace_if" << "reverse" << "reverse_copy" + << "search_n" << "shuffle" << "sort" << "sort_heap" << "stable_partition" << "stable_sort" << "swap_ranges" << "transform" << "unique" + << "unique_copy" << "upper_bound"; + const std::set algorithm22 = make_container< std::set >() // func(begin1 << end1 << begin2 << end2 + << "find_end" << "find_first_of" << "includes" << "lexicographical_compare" << "merge" << "partial_sort_copy" + << "search" << "set_difference" << "set_intersection" << "set_symmetric_difference" << "set_union"; + const std::set algorithm1x1 = make_container< std::set >() // func(begin1 << x << end1 + << "inplace_merge" << "nth_element" << "partial_sort" << "rotate" << "rotate_copy"; static const std::string iteratorBeginFuncPattern = "begin|cbegin|rbegin|crbegin"; static const std::string iteratorEndFuncPattern = "end|cend|rend|crend"; diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index b43becbe8..3d49d30ca 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -433,7 +433,7 @@ private: if (var2->isPointer()) checks.push_back(new UninitVar(owner, var2, symbolDatabase, library, isC)); else if (var2->typeEndToken()->str() != ">") { - bool stdtype = var2->typeStartToken()->isStandardType(); // TODO: change to isC to handle unknown types better + const bool stdtype = var2->typeStartToken()->isStandardType(); // TODO: change to isC to handle unknown types better if (stdtype && (!var2->isArray() || var2->nameToken()->linkAt(1)->strAt(1) == ";")) checks.push_back(new UninitVar(owner, var2, symbolDatabase, library, isC)); } @@ -571,7 +571,7 @@ private: return &tok; } - if (Token::Match(tok.next(), "= malloc|kmalloc") || Token::simpleMatch(tok.next(), "= new char [") || + if (Token::Match(tok.next(), "= malloc|kmalloc") || (!isC && Token::simpleMatch(tok.next(), "= new char [")) || (Token::Match(tok.next(), "= %name% (") && library->returnuninitdata.find(tok.strAt(2)) != library->returnuninitdata.end())) { alloc_pointer(checks, tok.varId()); if (tok.strAt(3) == "(") diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index 1526a0c99..d3295ab50 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -22,6 +22,7 @@ #include "tokenlist.h" #include "token.h" #include +#include class TestTokenList : public TestFixture { @@ -44,6 +45,13 @@ private: TokenList tokenlist(&settings); tokenlist.addtoken(code, 1, 1, false); ASSERT_EQUALS("9894494448401390090", tokenlist.front()->str()); + // that is supposed to break on 32bit + //unsigned long numberUL(0); + //std::istringstream(tokenlist.front()->str()) >> numberUL; + //ASSERT_EQUALS(9894494448401390090U, numberUL); + unsigned long long numberULL(0); + std::istringstream(tokenlist.front()->str()) >> numberULL; + ASSERT_EQUALS(9894494448401390090U, numberULL); } void line1() const {