CheckMemoryLeak: Move posix-opendir/closedir to library

This commit is contained in:
Daniel Marjamäki 2014-01-26 17:02:36 +01:00
parent 87b67e9b77
commit 2b8cf462c9
4 changed files with 7 additions and 107 deletions

View File

@ -8,5 +8,11 @@
<dealloc>close</dealloc>
<alloc init="true">socket</alloc>
</resource>
<resource>
<dealloc>closedir</dealloc>
<alloc init="true">opendir</alloc>
<alloc init="true">fdopendir</alloc>
</resource>
</def>

View File

@ -179,11 +179,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2,
if (Token::simpleMatch(tok2, "popen ("))
return Pipe;
if (settings1->standards.posix) {
if (Token::Match(tok2, "opendir|fdopendir ("))
return Dir;
}
// Does tok2 point on "g_malloc", "g_strdup", ..
const int alloctype = settings1->library.alloc(tok2->str());
if (alloctype > 0)
@ -267,9 +262,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
if (Token::Match(tok, "pclose ( %varid% )", varid))
return Pipe;
if (Token::Match(tok, "closedir ( %varid% )", varid))
return Dir;
}
// Does tok2 point on "g_free", etc ..
@ -311,9 +303,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
if (Token::simpleMatch(tok, std::string("pclose ( " + varname + " )").c_str()))
return Pipe;
if (Token::simpleMatch(tok, std::string("closedir ( " + varname + " )").c_str()))
return Dir;
if (Token::Match(tok, ("%type% ( " + varname + " )").c_str())) {
int type = settings1->library.dealloc(tok->str());
if (type > 0)
@ -333,7 +322,6 @@ void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, A
if (alloctype == CheckMemoryLeak::File ||
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir ||
alloctype == CheckMemoryLeak::OtherRes)
resourceLeakError(tok, varname);
else

View File

@ -91,7 +91,7 @@ public:
}
/** @brief What type of allocation are used.. the "Many" means that several types of allocation and deallocation are used */
enum AllocType { No, Malloc, New, NewArray, File, Fd, Pipe, Dir, OtherMem, OtherRes, Many };
enum AllocType { No, Malloc, New, NewArray, File, Fd, Pipe, OtherMem, OtherRes, Many };
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype);

View File

@ -204,7 +204,6 @@ private:
TEST_CASE(mismatch4);
TEST_CASE(mismatch5);
TEST_CASE(mismatch6);
TEST_CASE(mismatch7); // opendir()/closedir() on non-POSIX
TEST_CASE(mismatchSize);
@ -330,11 +329,6 @@ private:
TEST_CASE(close_function);
TEST_CASE(fd_functions);
TEST_CASE(opendir_function);
TEST_CASE(fdopendir_function);
TEST_CASE(closedir_function);
TEST_CASE(dir_functions);
TEST_CASE(pointer_to_pointer);
TEST_CASE(dealloc_and_alloc_in_func);
@ -1495,33 +1489,6 @@ private:
ASSERT_EQUALS("", errout.str());
}
void mismatch7() {
Settings settings;
settings.standards.posix = false;
const char mycode1[]= "DIR *opendir(const char *name);\n"
"void closedir(DIR *dir) {\n"
" free(dir);\n"
"}\n"
"\n"
"void f(const char *dir) {\n"
" DIR *dirp;\n"
" if ((dirp = opendir(dir)) == NULL) return 0;\n"
" closedir(dirp);\n"
"}\n";
check(mycode1, &settings);
ASSERT_EQUALS("", errout.str());
settings.standards.posix = true;
check(mycode1, &settings);
ASSERT_EQUALS("", errout.str());
const char mycode2[]= "void f(const char *dir) {\n"
" DIR *dirp;\n"
" if ((dirp = opendir(dir)) == NULL) return 0;\n"
" free(dirp);\n"
"}\n";
check(mycode2, &settings);
ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: dirp\n", errout.str());
}
void mismatchSize() {
check("void f(char *buf)\n"
"{\n"
@ -3664,67 +3631,6 @@ private:
ASSERT_EQUALS("[test.cpp:24]: (error) Resource leak: fd\n", errout.str());
}
void opendir_function() {
Settings settings;
settings.standards.posix = true;
check("void f()\n"
"{\n"
" DIR *f = opendir(\".\");\n"
"}", &settings);
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
}
void fdopendir_function() {
Settings settings;
settings.standards.posix = true;
check("void f(int fd)\n"
"{\n"
" DIR *f = fdopendir(fd);\n"
"}", &settings);
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
}
void closedir_function() {
Settings settings;
settings.standards.posix = true;
check("void f()\n"
"{\n"
" DIR *f = opendir(\".\");\n"
" closedir(f);\n"
"}", &settings);
ASSERT_EQUALS("", errout.str());
check("void f(int fd)\n"
"{\n"
" DIR *f = fdopendir(fd);\n"
" closedir(f);\n"
"}", &settings);
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" DIR * f = opendir(dirname);\n"
" if (closedir(f));\n"
"}", &settings);
ASSERT_EQUALS("", errout.str());
}
void dir_functions() {
Settings settings;
settings.standards.posix = true;
check("void f()\n"
"{\n"
" DIR *f = opendir(dir);\n"
" readdir(f);\n;"
" readdir_r(f, entry, res);\n;"
" rewinddir(f);\n;"
" telldir(f);\n;"
" seekdir(f, 2)\n;"
" scandir(f, namelist, filter, comp);\n;"
"}", &settings);
ASSERT_EQUALS("[test.cpp:10]: (error) Resource leak: f\n", errout.str());
}
void file_functions() {
check("void f()\n"
"{\n"