diff --git a/Makefile b/Makefile index c81588ea9..817ca09d5 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ OBJS=$(SRCS:%.cpp=%.o) all: ${OBJS} main.o g++ -Wall -g -o cppcheck $^ -test: ${OBJS} TestsRunner.o MiniCppUnit.o testmemleak.o testbufferoverrun.o +test: ${OBJS} TestsRunner.o MiniCppUnit.o testmemleak.o testbufferoverrun.o testdivision.o g++ -Wall -g -o testsrunner $^ clean: rm -f *.o cppcheck_test cppcheck diff --git a/testmemleak.cpp b/testmemleak.cpp index b1225666e..5f2105f93 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -53,6 +53,9 @@ public: TEST_CASE( mismatch1 ); + TEST_CASE( func1 ); + TEST_CASE( func2 ); + } void simple1() @@ -343,15 +346,83 @@ public: void mismatch1() { check( "void f()\n" - "{\n" - " int *a = new int[10];\n" - " free(a);\n" - "}\n"); + "{\n" + " int *a = new int[10];\n" + " free(a);\n" + "}\n"); ASSERT_EQUALS( std::string("[test.cpp:4]: Mismatching allocation and deallocation: a\n"), errout.str() ); } + + + //////////////////////////////////////////////// + // function calls + //////////////////////////////////////////////// + + + void func1() + { + check( "static void f()\n" + "{\n" + " char *p = new char[100];\n" + " foo(p);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + + void func2() + { + check( "static void f()\n" + "{\n" + " char *p = new char[100];\n" + " foo.add(p);\n" + "}\n" ); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + +/* + code = "static char *dmalloc()\n" + "{\n" + " char *p = new char[100];\n" + " return p;\n" + "}\n" + "static void f()\n" + "{\n" + " char *p = dmalloc();\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, code, "[test.cpp:9]: Memory leak: p\n" ); + + + code = "static char *dmalloc()\n" + "{\n" + " char *p = new char[100];\n" + " return p;\n" + "}\n" + "static void f()\n" + "{\n" + " char *p = dmalloc();\n" + " delete p;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, code, "[test.cpp:9]: Mismatching allocation and deallocation: p\n" ); + + + code = "static void foo(const char *str)\n" + "{ }\n" + "\n" + "static void f()\n" + "{\n" + " char *p = new char[100];\n" + " foo(p);\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: p\n" ); +*/ + + + }; REGISTER_FIXTURE( TestMemleak ) diff --git a/tests.cpp b/tests.cpp index a0eaa7c13..1b78959f4 100644 --- a/tests.cpp +++ b/tests.cpp @@ -26,7 +26,6 @@ static void constructors(); static void operator_eq(); static void memleak_in_function(); static void memleak_in_class(); -static void division(); static void variable_scope(); static void fpar_byvalue(); static void unused_struct_member(); @@ -52,9 +51,6 @@ int main() // Test that memory leaks in a class are detected memleak_in_class(); - // Check for dangerous division.. such as "svar / uvar". Treating "svar" as unsigned data is not good - division(); - // variable scope.. variable_scope(); @@ -187,23 +183,6 @@ static void operator_eq() static void memleak_in_function() { - - //////////////////////////////////////////////// - // for/while - //////////////////////////////////////////////// - - - - - - - - - - - - - //////////////////////////////////////////////// // Garbage collection //////////////////////////////////////////////// @@ -300,61 +279,6 @@ static void memleak_in_function() - //////////////////////////////////////////////// - // function calls - //////////////////////////////////////////////// - - - code = "static char *dmalloc()\n" - "{\n" - " char *p = new char[100];\n" - " return p;\n" - "}\n" - "static void f()\n" - "{\n" - " char *p = dmalloc();\n" - "}\n"; - check( CheckMemoryLeak, __LINE__, code, "[test.cpp:9]: Memory leak: p\n" ); - - - code = "static char *dmalloc()\n" - "{\n" - " char *p = new char[100];\n" - " return p;\n" - "}\n" - "static void f()\n" - "{\n" - " char *p = dmalloc();\n" - " delete p;\n" - "}\n"; - check( CheckMemoryLeak, __LINE__, code, "[test.cpp:9]: Mismatching allocation and deallocation: p\n" ); - - - code = "static void foo(const char *str)\n" - "{ }\n" - "\n" - "static void f()\n" - "{\n" - " char *p = new char[100];\n" - " foo(p);\n" - "}\n"; - check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: p\n" ); - - - code = "static void f()\n" - "{\n" - " char *p = new char[100];\n" - " foo(p);\n" - "}\n"; - check_( CheckMemoryLeak, __LINE__, code, "" ); - - - code = "static void f()\n" - "{\n" - " char *p = new char[100];\n" - " foo.add(p);\n" - "}\n"; - check_( CheckMemoryLeak, __LINE__, code, "" ); } //--------------------------------------------------------------------------- @@ -445,31 +369,6 @@ static void memleak_in_class() */ -} -//--------------------------------------------------------------------------- - -static void division() -{ - - const char *code; - - code = "void f()\n" - "{\n" - " int ivar = -2;\n" - " unsigned int uvar = 2;\n" - " return ivar / uvar;\n" - "}\n"; - check( CheckUnsignedDivision, __LINE__, code, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" ); - - - code = "void f()\n" - "{\n" - " int ivar = -2;\n" - " unsigned int uvar = 2;\n" - " return uvar / ivar;\n" - "}\n"; - check( CheckUnsignedDivision, __LINE__, code, "[test.cpp:5]: If the result is negative it will be wrong because an operand is unsigned.\n" ); - } //---------------------------------------------------------------------------