test: Added 'testdivision'
This commit is contained in:
parent
3d6093b970
commit
47d796519a
2
Makefile
2
Makefile
|
@ -7,7 +7,7 @@ OBJS=$(SRCS:%.cpp=%.o)
|
||||||
|
|
||||||
all: ${OBJS} main.o
|
all: ${OBJS} main.o
|
||||||
g++ -Wall -g -o cppcheck $^
|
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 $^
|
g++ -Wall -g -o testsrunner $^
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o cppcheck_test cppcheck
|
rm -f *.o cppcheck_test cppcheck
|
||||||
|
|
|
@ -53,6 +53,9 @@ public:
|
||||||
|
|
||||||
TEST_CASE( mismatch1 );
|
TEST_CASE( mismatch1 );
|
||||||
|
|
||||||
|
TEST_CASE( func1 );
|
||||||
|
TEST_CASE( func2 );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void simple1()
|
void simple1()
|
||||||
|
@ -343,15 +346,83 @@ public:
|
||||||
void mismatch1()
|
void mismatch1()
|
||||||
{
|
{
|
||||||
check( "void f()\n"
|
check( "void f()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int *a = new int[10];\n"
|
" int *a = new int[10];\n"
|
||||||
" free(a);\n"
|
" free(a);\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS( std::string("[test.cpp:4]: Mismatching allocation and deallocation: a\n"), errout.str() );
|
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 )
|
REGISTER_FIXTURE( TestMemleak )
|
||||||
|
|
101
tests.cpp
101
tests.cpp
|
@ -26,7 +26,6 @@ static void constructors();
|
||||||
static void operator_eq();
|
static void operator_eq();
|
||||||
static void memleak_in_function();
|
static void memleak_in_function();
|
||||||
static void memleak_in_class();
|
static void memleak_in_class();
|
||||||
static void division();
|
|
||||||
static void variable_scope();
|
static void variable_scope();
|
||||||
static void fpar_byvalue();
|
static void fpar_byvalue();
|
||||||
static void unused_struct_member();
|
static void unused_struct_member();
|
||||||
|
@ -52,9 +51,6 @@ int main()
|
||||||
// Test that memory leaks in a class are detected
|
// Test that memory leaks in a class are detected
|
||||||
memleak_in_class();
|
memleak_in_class();
|
||||||
|
|
||||||
// Check for dangerous division.. such as "svar / uvar". Treating "svar" as unsigned data is not good
|
|
||||||
division();
|
|
||||||
|
|
||||||
// variable scope..
|
// variable scope..
|
||||||
variable_scope();
|
variable_scope();
|
||||||
|
|
||||||
|
@ -187,23 +183,6 @@ static void operator_eq()
|
||||||
static void memleak_in_function()
|
static void memleak_in_function()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
// for/while
|
|
||||||
////////////////////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////
|
////////////////////////////////////////////////
|
||||||
// Garbage collection
|
// 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" );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue