Refactoring (use const std::set for strings). Omit some matching for C++ stuff within C code.

This commit is contained in:
Alexander Mai 2015-06-17 22:28:15 +02:00
parent eebb5b8c77
commit 13c1c2c035
5 changed files with 53 additions and 55 deletions

View File

@ -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<std::string> call_func_white_list = make_container < std::set<std::string> > ()
<< "_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"));
}

View File

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

View File

@ -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<std::string> algorithm2(algorithm2_strings, &algorithm2_strings[sizeof(algorithm2_strings) / sizeof(*algorithm2_strings)]);
static const std::set<std::string> algorithm22(algorithm22_strings, &algorithm22_strings[sizeof(algorithm22_strings) / sizeof(*algorithm22_strings)]);
static const std::set<std::string> algorithm1x1(algorithm1x1_strings, &algorithm1x1_strings[sizeof(algorithm1x1_strings) / sizeof(*algorithm1x1_strings)]);
const std::set<std::string> algorithm2 = make_container< std::set<std::string> >() // 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<std::string> algorithm22 = make_container< std::set<std::string> >() // 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<std::string> algorithm1x1 = make_container< std::set<std::string> >() // 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";

View File

@ -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) == "(")

View File

@ -22,6 +22,7 @@
#include "tokenlist.h"
#include "token.h"
#include <cstring>
#include <sstream>
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 {