From 0b5e6a55d44aa9bb7842fe0a1cbdffac607e5af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 17 Feb 2008 16:19:01 +0000 Subject: [PATCH] Unit Testing: Checking for mismatching allocation / deallocation --- CheckMemoryLeak.cpp | 17 ++++++++------ tests.cpp | 56 ++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index ad28348c1..b2d490100 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -326,7 +326,10 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) if (strcmp(getstr(func, 1), "::") != 0) 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; @@ -364,21 +367,21 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) if (match(tok, "delete var ;") && strcmp(getstr(tok,1),varname)==0) { - err |= ( Dealloc != No && Dealloc != New ); + err |= ( Alloc != No && Alloc != New ); Dealloc = New; } else if (match(tok, "delete [ ] var ;") && strcmp(getstr(tok,3),varname)==0) { - err |= ( Dealloc != No && Dealloc != NewA ); + err |= ( Alloc != No && Alloc != NewA ); Dealloc = NewA; } else if (match(tok, "free ( var )") && strcmp(getstr(tok,2),varname)==0) { - err |= ( Dealloc != No && Dealloc != Malloc ); + err |= ( Alloc != No && Alloc != Malloc ); Dealloc = Malloc; } @@ -408,7 +411,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) { if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) ) continue; - err |= ( Alloc != No && Alloc != New ); + err |= ( Dealloc != No && Dealloc != New ); Alloc = New; } @@ -416,7 +419,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) { if ( ! ShowAll && ! IsStandardType(getstr(tok,3)) ) continue; - err |= ( Alloc != No && Alloc != NewA ); + err |= ( Dealloc != No && Dealloc != NewA ); Alloc = NewA; } } @@ -429,7 +432,7 @@ static void _ClassMembers_CheckVar(const char *classname, const char *varname) ! IsStandardType(getstr(tok,3)) ) continue; - err |= ( Alloc != No && Alloc != Malloc ); + err |= ( Dealloc != No && Dealloc != Malloc ); Alloc = Malloc; } diff --git a/tests.cpp b/tests.cpp index 5df0c419e..c61ca2e93 100644 --- a/tests.cpp +++ b/tests.cpp @@ -15,9 +15,8 @@ #include //--------------------------------------------------------------------------- +bool ShowAll = true; bool Debug = false; -bool ShowAll = false; -bool CheckCodingStyle = false; //--------------------------------------------------------------------------- static unsigned int FailCount, SuccessCount; //--------------------------------------------------------------------------- @@ -25,7 +24,6 @@ static void internal_statementlist(); static void buffer_overrun(); static void constructors(); static void operator_eq(); -static void mismatching_allocation_deallocation(); static void memleak_in_function(); static void memleak_in_class(); //--------------------------------------------------------------------------- @@ -38,7 +36,6 @@ int main() constructors(); operator_eq(); memleak_in_function(); - mismatching_allocation_deallocation(); memleak_in_class(); std::cout << "Success Rate: " << 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() { // test1: 'new' but not 'delete' @@ -445,6 +427,9 @@ static void memleak_in_function() // test4: check all execution paths // test5: 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" "{\n" @@ -554,6 +539,16 @@ static void memleak_in_function() "}\n"; 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" ); + + + } //---------------------------------------------------------------------------