Refactoring: Refactoring of the unit testing for the CheckMemoryLeakInFunction

This commit is contained in:
Daniel Marjamäki 2009-08-30 21:11:15 +02:00
parent 6fb44e733b
commit 960fb861da
2 changed files with 26 additions and 128 deletions

View File

@ -43,7 +43,7 @@ static const char * const call_func_white_list[] =
{ {
"asprintf", "atof", "atoi", "atol", "clearerr", "delete", "fchmod", "fcntl" "asprintf", "atof", "atoi", "atol", "clearerr", "delete", "fchmod", "fcntl"
, "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets" , "fdatasync", "feof", "ferror", "fflush", "fgetc", "fgetpos", "fgets"
, "flock", "for", "fprintf", "fputc", "fputs", "fread", "fseek" , "flock", "for", "fprintf", "fputc", "fputs", "fread", "free", "fseek"
, "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate" , "fseeko", "fsetpos", "fstat", "fsync", "ftell", "ftello", "ftruncate"
, "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy" , "fwrite", "getc", "if", "ioctl", "lockf", "lseek", "memchr", "memcpy"
, "memmove", "memset", "posix_fadvise", "posix_fallocate", "pread" , "memmove", "memset", "posix_fadvise", "posix_fallocate", "pread"

View File

@ -148,9 +148,7 @@ private:
TEST_CASE(ifelse10); TEST_CASE(ifelse10);
TEST_CASE(if1); TEST_CASE(if1);
TEST_CASE(if3);
TEST_CASE(if4); TEST_CASE(if4);
TEST_CASE(if5);
TEST_CASE(if7); // Bug 2401436 TEST_CASE(if7); // Bug 2401436
TEST_CASE(if8); // Bug 2458532 TEST_CASE(if8); // Bug 2458532
TEST_CASE(if9); // if (realloc) TEST_CASE(if9); // if (realloc)
@ -167,9 +165,6 @@ private:
TEST_CASE(switch2); TEST_CASE(switch2);
TEST_CASE(switch3); TEST_CASE(switch3);
TEST_CASE(ret2);
TEST_CASE(ret3);
TEST_CASE(ret4);
TEST_CASE(ret5); // Bug 2458436 - return use TEST_CASE(ret5); // Bug 2458436 - return use
TEST_CASE(ret6); TEST_CASE(ret6);
TEST_CASE(ret7); TEST_CASE(ret7);
@ -180,14 +175,11 @@ private:
TEST_CASE(mismatch3); TEST_CASE(mismatch3);
TEST_CASE(mismatch4); TEST_CASE(mismatch4);
TEST_CASE(func1);
TEST_CASE(func2);
TEST_CASE(func3); TEST_CASE(func3);
TEST_CASE(func4); TEST_CASE(func4);
TEST_CASE(func5); TEST_CASE(func5);
TEST_CASE(func6); TEST_CASE(func6);
TEST_CASE(func7); TEST_CASE(func7);
TEST_CASE(func8); // Using callback
TEST_CASE(func9); // Embedding the function call in a if-condition TEST_CASE(func9); // Embedding the function call in a if-condition
TEST_CASE(func10); // Bug 2458510 - Function pointer TEST_CASE(func10); // Bug 2458510 - Function pointer
TEST_CASE(func11); // Bug 2458510 - Function pointer TEST_CASE(func11); // Bug 2458510 - Function pointer
@ -219,8 +211,6 @@ private:
TEST_CASE(varid); TEST_CASE(varid);
TEST_CASE(cast1); TEST_CASE(cast1);
TEST_CASE(cast2);
TEST_CASE(cast3);
// Using deallocated memory: // Using deallocated memory:
// * It is ok to take the address to deallocated memory // * It is ok to take the address to deallocated memory
@ -311,6 +301,10 @@ private:
return ret.str(); return ret.str();
} }
void testgetcode() void testgetcode()
{ {
// alloc; // alloc;
@ -329,8 +323,14 @@ private:
// dealloc; // dealloc;
ASSERT_EQUALS(";;dealloc;", getcode("char *s; free(s);", "s")); ASSERT_EQUALS(";;dealloc;", getcode("char *s; free(s);", "s"));
TODO_ASSERT_EQUALS(";;dealloc;", getcode("char *s; free((void *)s);", "s"));
TODO_ASSERT_EQUALS(";;dealloc;", getcode("char *s; free((void *)(s));", "s"));
TODO_ASSERT_EQUALS(";;dealloc;", getcode("char *s; free(reinterpret_cast<void *>(s));", "s"));
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete s;", "s")); ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete s;", "s"));
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete (s);", "s"));
TODO_ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete (void *)(s);", "s"));
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete [] s;", "s")); ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete [] s;", "s"));
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete [] (s);", "s"));
// if.. // if..
ASSERT_EQUALS(";;if{}", getcode("char *s; if (a) { }", "s")); ASSERT_EQUALS(";;if{}", getcode("char *s; if (a) { }", "s"));
@ -360,17 +360,24 @@ private:
TODO_ASSERT_EQUALS(";;alloc;", getcode("char *s; int ret; ret=asprintf(&s, \"xyz\"); if (ret==-1) return;", "s")); TODO_ASSERT_EQUALS(";;alloc;", getcode("char *s; int ret; ret=asprintf(&s, \"xyz\"); if (ret==-1) return;", "s"));
// use.. // use..
ASSERT_EQUALS(";;use;", getcode("char *s; DeleteString(s);", "s")); ASSERT_EQUALS(";;use;", getcode("char *s; a(s);", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; (*a)(s);", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; abc.a(s);", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; s2 = s;", "s")); ASSERT_EQUALS(";;use;", getcode("char *s; s2 = s;", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; s2 = s + 10;", "s")); ASSERT_EQUALS(";;use;", getcode("char *s; s2 = s + 10;", "s"));
// return..
ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s"));
ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s"));
// assign.. // assign..
ASSERT_EQUALS(";;assign;", getcode("char *s; s = 0;", "s")); ASSERT_EQUALS(";;assign;", getcode("char *s; s = 0;", "s"));
ASSERT_EQUALS(";;;", getcode("char *s; s = strcpy(s, p);", "s")); ASSERT_EQUALS(";;;", getcode("char *s; s = strcpy(s, p);", "s"));
// callfunc.. // callfunc..
ASSERT_EQUALS(";;assign" "callfunc;", getcode("char *s; s = a();", "s")); ASSERT_EQUALS(";;assigncallfunc;", getcode("char *s; s = a();", "s"));
ASSERT_EQUALS(";;callfunc;", getcode("char *s; a();", "s")); ASSERT_EQUALS(";;callfunc;", getcode("char *s; a();", "s"));
ASSERT_EQUALS(";;callfunc;", getcode("char *s; abc.a();", "s"));
// exit.. // exit..
ASSERT_EQUALS(";;exit;", getcode("char *s; exit(0);", "s")); ASSERT_EQUALS(";;exit;", getcode("char *s; exit(0);", "s"));
@ -438,6 +445,8 @@ private:
ASSERT_EQUALS("; alloc ;", simplifycode("; if { alloc; } else { return; }")); ASSERT_EQUALS("; alloc ;", simplifycode("; if { alloc; } else { return; }"));
ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode("; alloc ; if(!var) { alloc ; } dealloc ;")); ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode("; alloc ; if(!var) { alloc ; } dealloc ;"));
ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;"));
// "if ; .." // "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;")); ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;"));
@ -464,6 +473,10 @@ private:
// return.. // return..
// callfunc..
ASSERT_EQUALS("; callfunc ;", simplifycode(";callfunc;",false));
ASSERT_EQUALS(";", simplifycode(";callfunc;",true));
// exit.. // exit..
ASSERT_EQUALS(";", simplifycode("; if { alloc; exit; }")); ASSERT_EQUALS(";", simplifycode("; if { alloc; exit; }"));
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if { use; exit; }")); ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if { use; exit; }"));
@ -796,16 +809,6 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: p\n", errout.str());
} }
void if3()
{
check("void f()\n"
"{\n"
" char *s = new char[100];\n"
" if (0 != s)\n"
" foo(s);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void if4() void if4()
{ {
@ -821,18 +824,6 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void if5()
{
check("void f()\n"
"{\n"
" char *p = malloc(256);\n"
" if (somecondition && !p)\n"
" return;\n"
" free(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void if7() void if7()
{ {
check("void f( bool b )\n" check("void f( bool b )\n"
@ -1064,43 +1055,6 @@ private:
} }
void ret2()
{
check("void foo()\n"
"{\n"
" struct ABC *abc = new ABC;\n"
" abc->a = new char[10];\n"
" if ( ! abc->a )\n"
" return;\n"
" delete [] abc->a;\n"
" delete abc;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: abc\n", errout.str());
}
void ret3()
{
check("void foo()\n"
"{\n"
" FILE *filep = fopen(\"myfile.txt\",\"w\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: filep\n", errout.str());
}
void ret4()
{
check("void foo()\n"
"{\n"
" FILE *p = popen( \"ls -l\", \"r\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: p\n", errout.str());
}
void ret5() void ret5()
{ {
check("static char * f()\n" check("static char * f()\n"
@ -1205,27 +1159,6 @@ private:
//////////////////////////////////////////////// ////////////////////////////////////////////////
void func1()
{
check("static void f()\n"
"{\n"
" char *p = new char[100];\n"
" foo(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void func2()
{
check("static void f()\n"
"{\n"
" char *p = new char[100];\n"
" foo.add(p);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void func3() void func3()
{ {
@ -1308,17 +1241,6 @@ private:
} }
void func8()
{
check("static void foo()\n"
"{\n"
" char *str = new char[100];"
" (*release)(str);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void func9() void func9()
{ {
check("int b()\n" check("int b()\n"
@ -1703,30 +1625,6 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
} }
void cast2()
{
check("void foo()\n"
"{\n"
" char *a = malloc(10);\n"
" free((void *)a);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void cast3()
{
check("void foo()\n"
"{\n"
" char *a = malloc(10);\n"
" free(reinterpret_cast<void *>(a));\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void dealloc_use() void dealloc_use()