Fixed #1510 (false positive '(possible error) Memory leak' when 'a = b = new ...; delete a' (but not b))

This commit is contained in:
Daniel Marjamäki 2010-06-12 13:37:44 +02:00
parent 3a4a151cc4
commit 76221c0916
2 changed files with 35 additions and 10 deletions

View File

@ -2389,6 +2389,9 @@ void CheckMemoryLeakInClass::parseClass(const Token *tok1, std::vector<std::stri
void CheckMemoryLeakInClass::variable(const std::string &classname, const Token *tokVarname)
{
if (!_settings->inconclusive)
return;
const std::string varname = tokVarname->strAt(0);
// Check if member variable has been allocated and deallocated..

View File

@ -2849,6 +2849,7 @@ private:
TEST_CASE(class13);
TEST_CASE(class14);
TEST_CASE(class15);
TEST_CASE(class16);
TEST_CASE(staticvar);
@ -2884,8 +2885,8 @@ private:
"{\n"
" delete [] str2;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: Fred::str1\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: Fred::str1\n", errout.str());
}
@ -2910,7 +2911,8 @@ private:
" free(str1);\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()
@ -3057,7 +3059,8 @@ private:
" int * p;\n"
" A() { p = new int; }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
}
void class11()
@ -3070,7 +3073,8 @@ private:
"};\n"
"A::A() : p(new int[10])\n"
"{ }");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
}
void class12()
@ -3093,7 +3097,8 @@ private:
"\n"
"void A::cleanup()\n"
"{ delete [] p; }\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: A::p\n", errout.str());
}
void class13()
@ -3130,7 +3135,8 @@ private:
"\n"
"void A::init()\n"
"{ p = new int[10]; }\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
check("class A\n"
"{\n"
@ -3141,7 +3147,8 @@ private:
"\n"
"void A::init()\n"
"{ p = new int; }\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
check("class A\n"
"{\n"
@ -3152,7 +3159,8 @@ private:
"\n"
"void A::init()\n"
"{ p = malloc(sizeof(int)*10); }\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
ASSERT_EQUALS("", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: A::p\n", errout.str());
}
void class15()
@ -3191,6 +3199,19 @@ private:
ASSERT_EQUALS("", errout.str());
}
void class16()
{
// Ticket #1510
check("class A\n"
"{\n"
" int *a;\n"
" int *b;\n"
"public:\n"
" A() { a = b = new int[10]; }\n"
" ~A() { delete [] a; }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void staticvar()
{
@ -3291,7 +3312,8 @@ private:
"A::~A() {\n"
" delete [] pkt_buffer;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str());
ASSERT_EQUALS("", errout.str());
}
void func1()