Refactorizations in checkmemoryleak.cpp:
- Rely more on <alloc> declarations in Libraries - Removed unreachable debug message - Simplified code
This commit is contained in:
parent
0d25a43a5d
commit
2e7c5d37df
|
@ -118,21 +118,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
|
||||||
return No;
|
return No;
|
||||||
|
|
||||||
if (!Token::Match(tok2, "%name% ::|. %type%")) {
|
if (!Token::Match(tok2, "%name% ::|. %type%")) {
|
||||||
// Does tok2 point on "malloc", "strdup" or "kmalloc"..
|
|
||||||
static const char * const mallocfunc[] = {
|
|
||||||
"malloc",
|
|
||||||
"calloc",
|
|
||||||
"strdup",
|
|
||||||
"strndup",
|
|
||||||
"kmalloc",
|
|
||||||
"kzalloc",
|
|
||||||
"kcalloc"
|
|
||||||
};
|
|
||||||
for (unsigned int i = 0; i < sizeof(mallocfunc)/sizeof(*mallocfunc); i++) {
|
|
||||||
if (tok2->str() == mallocfunc[i])
|
|
||||||
return Malloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Using realloc..
|
// Using realloc..
|
||||||
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
|
if (varid && Token::Match(tok2, "realloc ( %any% ,") && tok2->tokAt(2)->varId() != varid)
|
||||||
return Malloc;
|
return Malloc;
|
||||||
|
@ -143,9 +128,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
|
||||||
return New;
|
return New;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Token::Match(tok2, "fopen|tmpfile|g_fopen ("))
|
|
||||||
return File;
|
|
||||||
|
|
||||||
if (settings1->standards.posix) {
|
if (settings1->standards.posix) {
|
||||||
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp|socket (")) {
|
if (Token::Match(tok2, "open|openat|creat|mkstemp|mkostemp|socket (")) {
|
||||||
// simple sanity check of function parameters..
|
// simple sanity check of function parameters..
|
||||||
|
@ -164,7 +146,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
|
||||||
return Pipe;
|
return Pipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does tok2 point on "g_malloc", "g_strdup", ..
|
// Does tok2 point on a Library allocation function?
|
||||||
const int alloctype = settings1->library.alloc(tok2);
|
const int alloctype = settings1->library.alloc(tok2);
|
||||||
if (alloctype > 0) {
|
if (alloctype > 0) {
|
||||||
if (alloctype == settings1->library.dealloc("free"))
|
if (alloctype == settings1->library.dealloc("free"))
|
||||||
|
@ -243,13 +225,9 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
|
||||||
vartok = vartok->tokAt(2);
|
vartok = vartok->tokAt(2);
|
||||||
|
|
||||||
if (Token::Match(vartok, "%varid% )|,|-", varid)) {
|
if (Token::Match(vartok, "%varid% )|,|-", varid)) {
|
||||||
if (Token::Match(tok, "free|kfree") ||
|
if (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))
|
||||||
(tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )")))
|
|
||||||
return Malloc;
|
return Malloc;
|
||||||
|
|
||||||
if (tok->str() == "fclose")
|
|
||||||
return File;
|
|
||||||
|
|
||||||
if (settings1->standards.posix) {
|
if (settings1->standards.posix) {
|
||||||
if (tok->str() == "close")
|
if (tok->str() == "close")
|
||||||
return Fd;
|
return Fd;
|
||||||
|
@ -257,10 +235,15 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
|
||||||
return Pipe;
|
return Pipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Does tok2 point on "g_free", etc ..
|
// Does tok point on a Library deallocation function?
|
||||||
const int dealloctype = settings1->library.dealloc(tok);
|
const int dealloctype = settings1->library.dealloc(tok);
|
||||||
if (dealloctype > 0)
|
if (dealloctype > 0) {
|
||||||
|
if (dealloctype == settings1->library.dealloc("free"))
|
||||||
|
return Malloc;
|
||||||
|
if (dealloctype == settings1->library.dealloc("fclose"))
|
||||||
|
return File;
|
||||||
return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
|
return Library::ismemory(dealloctype) ? OtherMem : OtherRes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2225,23 +2208,13 @@ void CheckMemoryLeakInClass::check()
|
||||||
if (!var->isStatic() && var->isPointer()) {
|
if (!var->isStatic() && var->isPointer()) {
|
||||||
// allocation but no deallocation of private variables in public function..
|
// allocation but no deallocation of private variables in public function..
|
||||||
const Token *tok = var->typeStartToken();
|
const Token *tok = var->typeStartToken();
|
||||||
if (tok->isStandardType()) {
|
// Either it is of standard type or a non-derived type
|
||||||
|
if (tok->isStandardType() || (var->type() && var->type()->derivedFrom.empty())) {
|
||||||
if (var->isPrivate())
|
if (var->isPrivate())
|
||||||
checkPublicFunctions(scope, var->nameToken());
|
checkPublicFunctions(scope, var->nameToken());
|
||||||
|
|
||||||
variable(scope, var->nameToken());
|
variable(scope, var->nameToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
// known class?
|
|
||||||
else if (var->type()) {
|
|
||||||
// not derived?
|
|
||||||
if (var->type()->derivedFrom.empty()) {
|
|
||||||
if (var->isPrivate())
|
|
||||||
checkPublicFunctions(scope, var->nameToken());
|
|
||||||
|
|
||||||
variable(scope, var->nameToken());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2378,10 +2351,6 @@ void CheckMemoryLeakInClass::checkPublicFunctions(const Scope *scope, const Toke
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const unsigned int varid = classtok->varId();
|
const unsigned int varid = classtok->varId();
|
||||||
if (varid == 0) {
|
|
||||||
_tokenizer->getSymbolDatabase()->debugMessage(classtok, "CheckMemoryInClass::checkPublicFunctions found variable \'" + classtok->str() + "\' with varid 0");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse public functions..
|
// Parse public functions..
|
||||||
// If they allocate member variables, they should also deallocate
|
// If they allocate member variables, they should also deallocate
|
||||||
|
|
|
@ -146,6 +146,7 @@ private:
|
||||||
void run() {
|
void run() {
|
||||||
LOAD_LIB_2(settings1.library, "std.cfg");
|
LOAD_LIB_2(settings1.library, "std.cfg");
|
||||||
LOAD_LIB_2(settings1.library, "gtk.cfg");
|
LOAD_LIB_2(settings1.library, "gtk.cfg");
|
||||||
|
LOAD_LIB_2(settings1.library, "posix.cfg");
|
||||||
LOAD_LIB_2(settings2.library, "std.cfg");
|
LOAD_LIB_2(settings2.library, "std.cfg");
|
||||||
|
|
||||||
// Check that getcode works correctly..
|
// Check that getcode works correctly..
|
||||||
|
@ -4209,7 +4210,7 @@ private:
|
||||||
check("void f() {\n"
|
check("void f() {\n"
|
||||||
" char *p;\n"
|
" char *p;\n"
|
||||||
" char **pp = &p;\n"
|
" char **pp = &p;\n"
|
||||||
" *pp = calloc(10);\n"
|
" *pp = calloc(10, 1);\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
|
||||||
}
|
}
|
||||||
|
@ -4295,6 +4296,8 @@ private:
|
||||||
settings.addEnabled("warning");
|
settings.addEnabled("warning");
|
||||||
settings.addEnabled("style");
|
settings.addEnabled("style");
|
||||||
|
|
||||||
|
LOAD_LIB_2(settings.library, "std.cfg");
|
||||||
|
|
||||||
TEST_CASE(class1);
|
TEST_CASE(class1);
|
||||||
TEST_CASE(class2);
|
TEST_CASE(class2);
|
||||||
TEST_CASE(class3);
|
TEST_CASE(class3);
|
||||||
|
@ -5550,6 +5553,9 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() {
|
void run() {
|
||||||
|
LOAD_LIB_2(settings.library, "std.cfg");
|
||||||
|
LOAD_LIB_2(settings.library, "posix.cfg");
|
||||||
|
|
||||||
// testing that errors are detected
|
// testing that errors are detected
|
||||||
TEST_CASE(err);
|
TEST_CASE(err);
|
||||||
|
|
||||||
|
@ -5985,6 +5991,7 @@ private:
|
||||||
settings.addEnabled("warning");
|
settings.addEnabled("warning");
|
||||||
|
|
||||||
LOAD_LIB_2(settings.library, "std.cfg");
|
LOAD_LIB_2(settings.library, "std.cfg");
|
||||||
|
LOAD_LIB_2(settings.library, "posix.cfg");
|
||||||
LOAD_LIB_2(settings.library, "gtk.cfg");
|
LOAD_LIB_2(settings.library, "gtk.cfg");
|
||||||
|
|
||||||
// pass allocated memory to function..
|
// pass allocated memory to function..
|
||||||
|
@ -6095,7 +6102,7 @@ private:
|
||||||
|
|
||||||
check("void x()\n"
|
check("void x()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" calloc(10);\n"
|
" calloc(10, 1);\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'calloc' is not stored.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'calloc' is not stored.\n", errout.str());
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,8 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() {
|
void run() {
|
||||||
|
LOAD_LIB_2(settings.library, "std.cfg");
|
||||||
|
|
||||||
TEST_CASE(deadlock_with_many_errors);
|
TEST_CASE(deadlock_with_many_errors);
|
||||||
TEST_CASE(many_threads);
|
TEST_CASE(many_threads);
|
||||||
TEST_CASE(no_errors_more_files);
|
TEST_CASE(no_errors_more_files);
|
||||||
|
|
Loading…
Reference in New Issue