Unit Testing: Checking for mismatching allocation / deallocation
This commit is contained in:
parent
ac6ce3a0d1
commit
0b5e6a55d4
|
@ -326,7 +326,10 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
|
||||||
if (strcmp(getstr(func, 1), "::") != 0)
|
if (strcmp(getstr(func, 1), "::") != 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(getstr(func, 3), "(") != 0)
|
if ((strcmp(getstr(func,2),"~")==0 &&
|
||||||
|
strcmp(getstr(func,3),classname)==0 &&
|
||||||
|
strcmp(getstr(func,4),"(")==0) ||
|
||||||
|
(strcmp(getstr(func, 3), "(") != 0))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
|
@ -364,21 +367,21 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
|
||||||
if (match(tok, "delete var ;") &&
|
if (match(tok, "delete var ;") &&
|
||||||
strcmp(getstr(tok,1),varname)==0)
|
strcmp(getstr(tok,1),varname)==0)
|
||||||
{
|
{
|
||||||
err |= ( Dealloc != No && Dealloc != New );
|
err |= ( Alloc != No && Alloc != New );
|
||||||
Dealloc = New;
|
Dealloc = New;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (match(tok, "delete [ ] var ;") &&
|
else if (match(tok, "delete [ ] var ;") &&
|
||||||
strcmp(getstr(tok,3),varname)==0)
|
strcmp(getstr(tok,3),varname)==0)
|
||||||
{
|
{
|
||||||
err |= ( Dealloc != No && Dealloc != NewA );
|
err |= ( Alloc != No && Alloc != NewA );
|
||||||
Dealloc = NewA;
|
Dealloc = NewA;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (match(tok, "free ( var )") &&
|
else if (match(tok, "free ( var )") &&
|
||||||
strcmp(getstr(tok,2),varname)==0)
|
strcmp(getstr(tok,2),varname)==0)
|
||||||
{
|
{
|
||||||
err |= ( Dealloc != No && Dealloc != Malloc );
|
err |= ( Alloc != No && Alloc != Malloc );
|
||||||
Dealloc = Malloc;
|
Dealloc = Malloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -408,7 +411,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
|
||||||
{
|
{
|
||||||
if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
|
if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
|
||||||
continue;
|
continue;
|
||||||
err |= ( Alloc != No && Alloc != New );
|
err |= ( Dealloc != No && Dealloc != New );
|
||||||
Alloc = New;
|
Alloc = New;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,7 +419,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
|
||||||
{
|
{
|
||||||
if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
|
if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) )
|
||||||
continue;
|
continue;
|
||||||
err |= ( Alloc != No && Alloc != NewA );
|
err |= ( Dealloc != No && Dealloc != NewA );
|
||||||
Alloc = NewA;
|
Alloc = NewA;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -429,7 +432,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname)
|
||||||
! IsStandardType(getstr(tok,3)) )
|
! IsStandardType(getstr(tok,3)) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
err |= ( Alloc != No && Alloc != Malloc );
|
err |= ( Dealloc != No && Dealloc != Malloc );
|
||||||
Alloc = Malloc;
|
Alloc = Malloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
56
tests.cpp
56
tests.cpp
|
@ -15,9 +15,8 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
bool ShowAll = true;
|
||||||
bool Debug = false;
|
bool Debug = false;
|
||||||
bool ShowAll = false;
|
|
||||||
bool CheckCodingStyle = false;
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
static unsigned int FailCount, SuccessCount;
|
static unsigned int FailCount, SuccessCount;
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
@ -25,7 +24,6 @@ static void internal_statementlist();
|
||||||
static void buffer_overrun();
|
static void buffer_overrun();
|
||||||
static void constructors();
|
static void constructors();
|
||||||
static void operator_eq();
|
static void operator_eq();
|
||||||
static void mismatching_allocation_deallocation();
|
|
||||||
static void memleak_in_function();
|
static void memleak_in_function();
|
||||||
static void memleak_in_class();
|
static void memleak_in_class();
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
@ -38,7 +36,6 @@ int main()
|
||||||
constructors();
|
constructors();
|
||||||
operator_eq();
|
operator_eq();
|
||||||
memleak_in_function();
|
memleak_in_function();
|
||||||
mismatching_allocation_deallocation();
|
|
||||||
memleak_in_class();
|
memleak_in_class();
|
||||||
std::cout << "Success Rate: "
|
std::cout << "Success Rate: "
|
||||||
<< SuccessCount
|
<< SuccessCount
|
||||||
|
@ -422,21 +419,6 @@ void operator_eq()
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
static void mismatching_allocation_deallocation()
|
|
||||||
{
|
|
||||||
// TODO: This check must be created as I can't find it anywhere
|
|
||||||
|
|
||||||
/*
|
|
||||||
const char test1[] = "void f()\n"
|
|
||||||
"{\n"
|
|
||||||
" int *a = new int[10];\n"
|
|
||||||
" free(a);\n"
|
|
||||||
"}\n";
|
|
||||||
check( CheckMismatchingAllocationDeallocation, __LINE__, test1, "[test.cpp:4]: Mismatching allocation / deallocation\n" );
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
static void memleak_in_function()
|
static void memleak_in_function()
|
||||||
{
|
{
|
||||||
// test1: 'new' but not 'delete'
|
// test1: 'new' but not 'delete'
|
||||||
|
@ -445,6 +427,9 @@ static void memleak_in_function()
|
||||||
// test4: check all execution paths
|
// test4: check all execution paths
|
||||||
// test5: check all execution paths
|
// test5: check all execution paths
|
||||||
// test6: check all execution paths
|
// test6: check all execution paths
|
||||||
|
// test7: check all execution paths
|
||||||
|
// test8: check all execution paths
|
||||||
|
// test9: mismatching allocation / deallocation
|
||||||
|
|
||||||
const char test1[] = "void f()\n"
|
const char test1[] = "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
|
@ -554,6 +539,16 @@ static void memleak_in_function()
|
||||||
"}\n";
|
"}\n";
|
||||||
check( CheckMemoryLeak, __LINE__, test8, "" );
|
check( CheckMemoryLeak, __LINE__, test8, "" );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const char test9[] = "void f()\n"
|
||||||
|
"{\n"
|
||||||
|
" int *a = new int[10];\n"
|
||||||
|
" free(a);\n"
|
||||||
|
"}\n";
|
||||||
|
check( CheckMemoryLeak, __LINE__, test9, "[test.cpp:4]: Mismatching allocation and deallocation 'a'\n" );
|
||||||
|
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -587,6 +582,29 @@ static void memleak_in_class()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const char test2[] = "class clKalle\n"
|
||||||
|
"{\n"
|
||||||
|
"private:\n"
|
||||||
|
" char *str1;\n"
|
||||||
|
"public:\n"
|
||||||
|
" clKalle();\n"
|
||||||
|
" ~clKalle();\n"
|
||||||
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"clKalle::clKalle()\n"
|
||||||
|
"{\n"
|
||||||
|
" str1 = new char[10];\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"clKalle::~clKalle()\n"
|
||||||
|
"{\n"
|
||||||
|
" free(str1);\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
check( CheckMemoryLeak, __LINE__, test2, "[test.cpp:17]: Mismatching deallocation for 'clKalle::str1'\n" );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue