Memory leaks: more specific bailouts to reduce false negatives

This commit is contained in:
Daniel Marjamäki 2010-06-13 19:00:11 +02:00
parent 37ade20dad
commit e987d2e05b
2 changed files with 19 additions and 22 deletions

View File

@ -2389,9 +2389,6 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
void CheckMemoryLeakInClass::variable(const std::string &classname, const Token *tokVarname) void CheckMemoryLeakInClass::variable(const std::string &classname, const Token *tokVarname)
{ {
if (!_settings->inconclusive)
return;
const std::string varname = tokVarname->strAt(0); const std::string varname = tokVarname->strAt(0);
// Check if member variable has been allocated and deallocated.. // Check if member variable has been allocated and deallocated..
@ -2434,6 +2431,16 @@ void CheckMemoryLeakInClass::variable(const std::string &classname, const Token
// Allocate.. // Allocate..
if (indent == 0 || Token::Match(tok, (varname + " =").c_str())) if (indent == 0 || Token::Match(tok, (varname + " =").c_str()))
{ {
// var1 = var2 = ...
// bail out
if (Token::simpleMatch(tok->previous(), "="))
return;
// Foo::var1 = ..
// bail out
if (Token::simpleMatch(tok->previous(), "::"))
return;
AllocType alloc = getAllocationType(tok->tokAt((indent > 0) ? 2 : 3), 0); AllocType alloc = getAllocationType(tok->tokAt((indent > 0) ? 2 : 3), 0);
if (alloc != CheckMemoryLeak::No) if (alloc != CheckMemoryLeak::No)
{ {

View File

@ -2886,8 +2886,7 @@ private:
"{\n" "{\n"
" delete [] str2;\n" " delete [] str2;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: Fred::str1\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: Fred::str1\n", errout.str());
} }
@ -2911,9 +2910,7 @@ private:
"{\n" "{\n"
" free(str1);\n" " free(str1);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:17]: (error) Mismatching allocation and deallocation: Fred::str1\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:17]: (error) Mismatching allocation and deallocation: Fred::str1\n", errout.str());
} }
void class3() void class3()
@ -3060,8 +3057,7 @@ private:
" int * p;\n" " int * p;\n"
" A() { p = new int; }\n" " A() { p = new int; }\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
} }
void class11() void class11()
@ -3074,8 +3070,7 @@ private:
"};\n" "};\n"
"A::A() : p(new int[10])\n" "A::A() : p(new int[10])\n"
"{ }"); "{ }");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
} }
void class12() void class12()
@ -3098,8 +3093,7 @@ private:
"\n" "\n"
"void A::cleanup()\n" "void A::cleanup()\n"
"{ delete [] p; }\n"); "{ delete [] p; }\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
} }
void class13() void class13()
@ -3136,8 +3130,7 @@ private:
"\n" "\n"
"void A::init()\n" "void A::init()\n"
"{ p = new int[10]; }\n"); "{ p = new int[10]; }\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
check("class A\n" check("class A\n"
"{\n" "{\n"
@ -3148,8 +3141,7 @@ private:
"\n" "\n"
"void A::init()\n" "void A::init()\n"
"{ p = new int; }\n"); "{ p = new int; }\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
check("class A\n" check("class A\n"
"{\n" "{\n"
@ -3160,8 +3152,7 @@ private:
"\n" "\n"
"void A::init()\n" "void A::init()\n"
"{ p = malloc(sizeof(int)*10); }\n"); "{ p = malloc(sizeof(int)*10); }\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
} }
void class15() void class15()
@ -3331,8 +3322,7 @@ private:
"A::~A() {\n" "A::~A() {\n"
" delete [] pkt_buffer;\n" " delete [] pkt_buffer;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str()); ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str());
ASSERT_EQUALS("", errout.str());
} }
void func1() void func1()