From 2dace08aaecdf6f5e7f42e449e4871c6ca236934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 16 Feb 2008 18:24:13 +0000 Subject: [PATCH] Unit Testing: Moved 'testmemcheck' --- CheckMemoryLeak.cpp | 4 ++ main.cpp | 6 --- testmemcheck1/err.msg | 1 - testmemcheck1/testmemcheck1.cpp | 7 --- testmemcheck2/err.msg | 0 testmemcheck2/testmemcheck2.cpp | 7 --- testmemcheck3/err.msg | 0 testmemcheck3/testmemcheck3.cpp | 18 ------- testmemcheck4/err.msg | 1 - testmemcheck4/testmemcheck4.cpp | 13 ----- testmemcheck5/err.msg | 0 testmemcheck5/testmemcheck5.cpp | 13 ----- tests.cpp | 96 +++++++++++++++++++++++++++++++++ 13 files changed, 100 insertions(+), 66 deletions(-) delete mode 100644 testmemcheck1/err.msg delete mode 100644 testmemcheck1/testmemcheck1.cpp delete mode 100644 testmemcheck2/err.msg delete mode 100644 testmemcheck2/testmemcheck2.cpp delete mode 100644 testmemcheck3/err.msg delete mode 100644 testmemcheck3/testmemcheck3.cpp delete mode 100644 testmemcheck4/err.msg delete mode 100644 testmemcheck4/testmemcheck4.cpp delete mode 100644 testmemcheck5/err.msg delete mode 100644 testmemcheck5/testmemcheck5.cpp diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 6f56737a3..ad28348c1 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -498,6 +498,10 @@ static void _ClassMembers() void CheckMemoryLeak() { + // Create statement list. + Statements.clear(); + CreateStatementList(); + // Check for memory leaks inside functions.. _InFunction(); diff --git a/main.cpp b/main.cpp index cb7926bde..12ea973a8 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,6 @@ #include #include "tokenize.h" // <- Tokenizer -#include "Statements.h" // <- Statement list #include "CommonCheck.h" #include "CheckMemoryLeak.h" @@ -99,11 +98,6 @@ static void CppCheck(const char FileName[]) SimplifyTokenList(); - - // Create a statement list. It's used by for example 'CheckMemoryLeak' - CreateStatementList(); - - // Memory leak CheckMemoryLeak(); diff --git a/testmemcheck1/err.msg b/testmemcheck1/err.msg deleted file mode 100644 index 740085451..000000000 --- a/testmemcheck1/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testmemcheck1\testmemcheck1.cpp:5]: Mismatching allocation and deallocation 'a' diff --git a/testmemcheck1/testmemcheck1.cpp b/testmemcheck1/testmemcheck1.cpp deleted file mode 100644 index 20f51b386..000000000 --- a/testmemcheck1/testmemcheck1.cpp +++ /dev/null @@ -1,7 +0,0 @@ - -void f() -{ - int *a = new int[10]; - delete a; -} - diff --git a/testmemcheck2/err.msg b/testmemcheck2/err.msg deleted file mode 100644 index e69de29bb..000000000 diff --git a/testmemcheck2/testmemcheck2.cpp b/testmemcheck2/testmemcheck2.cpp deleted file mode 100644 index 1a639ee07..000000000 --- a/testmemcheck2/testmemcheck2.cpp +++ /dev/null @@ -1,7 +0,0 @@ - -Fred *newfred() -{ - Fred *f = new Fred(); - return f; -} - diff --git a/testmemcheck3/err.msg b/testmemcheck3/err.msg deleted file mode 100644 index e69de29bb..000000000 diff --git a/testmemcheck3/testmemcheck3.cpp b/testmemcheck3/testmemcheck3.cpp deleted file mode 100644 index 8c74cc138..000000000 --- a/testmemcheck3/testmemcheck3.cpp +++ /dev/null @@ -1,18 +0,0 @@ - - - - -void f() -{ - Kalle *kalle; - if (somecondition) - { - kalle = new Kalle; - } - else - { - return; - } - delete kalle; -} - diff --git a/testmemcheck4/err.msg b/testmemcheck4/err.msg deleted file mode 100644 index 2ef99ae08..000000000 --- a/testmemcheck4/err.msg +++ /dev/null @@ -1 +0,0 @@ -[testmemcheck4\testmemcheck4.cpp:9]: Memory leak:str diff --git a/testmemcheck4/testmemcheck4.cpp b/testmemcheck4/testmemcheck4.cpp deleted file mode 100644 index da52a4ae2..000000000 --- a/testmemcheck4/testmemcheck4.cpp +++ /dev/null @@ -1,13 +0,0 @@ - - -void f() -{ - for (int i = 0; i < j; i++) - { - char *str = strdup("hello"); - if (condition) - continue; - free(str); - } -} - diff --git a/testmemcheck5/err.msg b/testmemcheck5/err.msg deleted file mode 100644 index e69de29bb..000000000 diff --git a/testmemcheck5/testmemcheck5.cpp b/testmemcheck5/testmemcheck5.cpp deleted file mode 100644 index c4c2cb0ca..000000000 --- a/testmemcheck5/testmemcheck5.cpp +++ /dev/null @@ -1,13 +0,0 @@ - - -void f() -{ - char *str = strdup("hello"); - while (condition) - { - if (condition) - break; - } - free(str); -} - diff --git a/tests.cpp b/tests.cpp index 50ab9b500..7e94ad9bd 100644 --- a/tests.cpp +++ b/tests.cpp @@ -8,6 +8,7 @@ #include "CheckBufferOverrun.h" #include "CheckClass.h" +#include "CheckMemoryLeak.h" #include #include @@ -22,6 +23,8 @@ static unsigned int FailCount, SuccessCount; static void buffer_overrun(); static void constructors(); static void operator_eq(); +static void mismatching_allocation_deallocation(); +static void memleak_in_function(); //--------------------------------------------------------------------------- int main() @@ -30,6 +33,8 @@ int main() buffer_overrun(); constructors(); operator_eq(); + memleak_in_function(); + mismatching_allocation_deallocation(); std::cout << "Success Rate: " << SuccessCount << " / " @@ -241,3 +246,94 @@ 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' + // test2: Return allocated memory + // test3: check all execution paths + // test4: check all execution paths + // test5: check all execution paths + + const char test1[] = "void f()\n" + "{\n" + " int *a = new int[10];\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test1, "[test.cpp:4]: Memory leak:a\n" ); + + + + + const char test2[] = "Fred *NewFred()\n" + "{\n" + " Fred *f = new Fred;\n" + " return f;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test2, "" ); + + + + + + const char test3[] = "void f()\n" + "{\n" + " Kalle *kalle;\n" + " if (somecondition)\n" + " {\n" + " kalle = new Kalle;\n" + " }\n" + " else\n" + " {\n" + " return;\n" + " }\n" + " delete kalle;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test3, "" ); + + + + const char test4[] = "void f()\n" + "{\n" + " for (int i = 0; i < j; i++)\n" + " {\n" + " char *str = strdup(\"hello\");\n" + " if (condition)\n" + " continue;\n" + " free(str);\n" + " }\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test4, "[test.cpp:7]: Memory leak:str\n" ); + + + + + + const char test5[] = "void f()\n" + "{\n" + " char *str = strdup(\"hello\");\n" + " while (condition)\n" + " {\n" + " if (condition)\n" + " break;\n" + " }\n" + " free(str);\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test5, "" ); + +} +//--------------------------------------------------------------------------- +