Fixed #3267 (False positive (experimental): resource leak in vdr-xinelib)

This commit is contained in:
Daniel Marjamäki 2011-11-30 19:43:02 +01:00
parent 414e0ecc3c
commit dcd3f4fe3d
4 changed files with 102 additions and 139 deletions

View File

@ -308,15 +308,15 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
//--------------------------------------------------------------------------
void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype, bool inconclusive)
void CheckMemoryLeak::memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype)
{
if (alloctype == CheckMemoryLeak::File ||
alloctype == CheckMemoryLeak::Pipe ||
alloctype == CheckMemoryLeak::Fd ||
alloctype == CheckMemoryLeak::Dir)
resourceLeakError(tok, varname.c_str(), inconclusive);
resourceLeakError(tok, varname.c_str());
else
memleakError(tok, varname.c_str(), inconclusive);
memleakError(tok, varname.c_str());
}
//---------------------------------------------------------------------------
@ -353,31 +353,9 @@ void CheckMemoryLeak::reportErr(const std::list<const Token *> &callstack, Sever
Check::reportError(errmsg);
}
void CheckMemoryLeak::reportInconclusiveError(const Token *tok, Severity::SeverityType severity, const std::string &id, const std::string &msg) const
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname)
{
std::list<ErrorLogger::ErrorMessage::FileLocation> locations;
if (tok) {
ErrorLogger::ErrorMessage::FileLocation loc;
loc.line = tok->linenr();
loc.setfile(tokenizer->file(tok));
locations.push_back(loc);
}
const ErrorLogger::ErrorMessage errmsg(locations, severity, msg, id, true);
if (errorLogger)
errorLogger->reportErr(errmsg);
else
Check::reportError(errmsg);
}
void CheckMemoryLeak::memleakError(const Token *tok, const std::string &varname, bool inconclusive)
{
if (inconclusive)
reportInconclusiveError(tok, Severity::error, "memleak", "Memory leak: " + varname + " (this might be a false warning)");
else
reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname);
reportErr(tok, Severity::error, "memleak", "Memory leak: " + varname);
}
void CheckMemoryLeak::memleakUponReallocFailureError(const Token *tok, const std::string &varname)
@ -385,15 +363,12 @@ void CheckMemoryLeak::memleakUponReallocFailureError(const Token *tok, const std
reportErr(tok, Severity::error, "memleakOnRealloc", "Common realloc mistake: \'" + varname + "\' nulled but not freed upon failure");
}
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname, bool inconclusive)
void CheckMemoryLeak::resourceLeakError(const Token *tok, const std::string &varname)
{
std::string errmsg("Resource leak");
if (!varname.empty())
errmsg += ": " + varname;
if (inconclusive)
reportInconclusiveError(tok, Severity::error, "resourceLeak", errmsg + " (this might be a false warning)");
else
reportErr(tok, Severity::error, "resourceLeak", errmsg);
reportErr(tok, Severity::error, "resourceLeak", errmsg);
}
void CheckMemoryLeak::deallocDeallocError(const Token *tok, const std::string &varname)
@ -1698,7 +1673,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok)
}
// Delete "if { dealloc|assign|use ; return ; }"
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }")) {
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") &&
!Token::findmatch(tok, "if alloc ;")) {
Token::eraseTokens(tok2, tok2->tokAt(8));
if (Token::simpleMatch(tok2->next(), "else"))
tok2->deleteNext();
@ -2185,7 +2161,7 @@ void CheckMemoryLeakInFunction::checkScope(const Token *Tok1, const std::string
}
if ((result = findleak(tok)) != NULL) {
memoryLeak(result, varname, alloctype, true);
memoryLeak(result, varname, alloctype);
}
else if ((result = Token::findsimplematch(tok, "dealloc ; dealloc ;")) != NULL) {
@ -2389,10 +2365,6 @@ void CheckMemoryLeakInFunction::parseFunctionScope(const Token *tok, const Token
void CheckMemoryLeakInFunction::check()
{
// inconclusive checks. See #3267 and #3268
if (!_settings->inconclusive)
return;
// fill the "noreturn"
parse_noreturn();
@ -2596,9 +2568,9 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam
}
if (allocInConstructor && !deallocInDestructor) {
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc, false);
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc);
} else if (Alloc != CheckMemoryLeak::No && Dealloc == CheckMemoryLeak::No) {
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc, false);
memoryLeak(tokVarname, (classname + "::" + varname).c_str(), Alloc);
}
}
@ -2741,7 +2713,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
else if (tok3->str() == "}") {
if (indentlevel3 == 0) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
break;
}
--indentlevel3;
@ -2773,7 +2745,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
// Deallocating the struct..
else if (indentlevel2 == 0 && Token::Match(tok3, "free|kfree ( %varid% )", structid)) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
break;
}
@ -2819,7 +2791,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok
// Returning from function without deallocating struct member?
if (!Token::Match(tok3, "return %varid% ;", structid) &&
!Token::Match(tok3, "return & %varid% .", structid)) {
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc, false);
memoryLeak(tok3, (vartok->str() + "." + tok2->strAt(2)).c_str(), Malloc);
}
break;
}

View File

@ -82,15 +82,6 @@ private:
*/
void reportErr(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
/**
* Report inconclusive error. Similar with the function Check::reportInconclusiveError
* @param location the token where the error occurs
* @param severity the severity of the bug
* @param id type of message
* @param msg text
*/
void reportInconclusiveError(const Token *location, Severity::SeverityType severity, const std::string &id, const std::string &msg) const;
public:
CheckMemoryLeak(const Tokenizer *t, ErrorLogger *e)
: tokenizer(t), errorLogger(e) {
@ -100,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, gMalloc, New, NewArray, File, Fd, Pipe, Dir, Many };
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype, bool inconclusive);
void memoryLeak(const Token *tok, const std::string &varname, AllocType alloctype);
/**
* @brief Get type of deallocation at given position
@ -142,14 +133,14 @@ public:
* @param tok token where memory is leaked
* @param varname name of variable
*/
void memleakError(const Token *tok, const std::string &varname, bool inconclusive);
void memleakError(const Token *tok, const std::string &varname);
/**
* Report that there is a resource leak (fopen/popen/etc)
* @param tok token where resource is leaked
* @param varname name of variable
*/
void resourceLeakError(const Token *tok, const std::string &varname, bool inconclusive);
void resourceLeakError(const Token *tok, const std::string &varname);
/**
* @brief Report error: deallocating a deallocated pointer
@ -321,8 +312,8 @@ public:
void getErrorMessages(ErrorLogger *e, const Settings *settings) {
CheckMemoryLeakInFunction c(0, settings, e);
c.memleakError(0, "varname", false);
c.resourceLeakError(0, "varname", false);
c.memleakError(0, "varname");
c.resourceLeakError(0, "varname");
c.deallocDeallocError(0, "varname");
c.deallocuseError(0, "varname");

View File

@ -127,7 +127,6 @@ private:
Settings settings;
settings.experimental = experimental;
settings.inconclusive = true;
// Tokenize..
Tokenizer tokenizer(&settings, this);
@ -705,6 +704,7 @@ private:
ASSERT_EQUALS("; loop use ;", simplifycode("; loop { loop loop use ; } ;"));
ASSERT_EQUALS("; }", simplifycode("; loop { if break ; break ; } ; }"));
ASSERT_EQUALS("; }", simplifycode("; loop { if continue ; if continue ; } ; }"));
ASSERT_EQUALS("; loop if alloc ; if { dealloc ; return ; } }", simplifycode("; loop { if alloc ; } if { dealloc ; return ; } }"));
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; while(!var) alloc ;"));
@ -909,27 +909,27 @@ private:
"{\n"
" int *p = new(std::nothrow) int;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" using std::nothrow;\n"
" int *p = new(nothrow) int;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" int *p = new(std::nothrow) int[10];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
" using namespace std;\n"
" int *p = new(nothrow) int[10];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
check("void f()\n"
"{\n"
@ -1004,7 +1004,7 @@ private:
" str = new char[20];\n"
" delete [] str;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str\n", errout.str());
}
@ -1026,7 +1026,7 @@ private:
" }\n"
" return NULL;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: s (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: s\n", errout.str());
}
@ -1119,7 +1119,7 @@ private:
" }\n"
" free(c);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: c (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: c\n", errout.str());
}
void if9() {
@ -1160,7 +1160,7 @@ private:
" }\n"
" delete [] x;\n"
"}\n", true);
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x (this might be a false warning)\n",
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n",
"", errout.str());
}
@ -1188,7 +1188,7 @@ private:
" str = strdup(a[i]);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: str (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: str\n", errout.str());
}
@ -1210,7 +1210,7 @@ private:
"\n"
" return a;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: a (this might be a false warning)\n",
TODO_ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: a\n",
"[test.cpp:8]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n",
errout.str());
@ -1254,7 +1254,7 @@ private:
" return a;\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:9]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n"
"[test.cpp:11]: (error) Memory leak: a (this might be a false warning)\n", errout.str());
"[test.cpp:11]: (error) Memory leak: a\n", errout.str());
}
@ -1286,7 +1286,7 @@ private:
" };\n"
"}\n");
check(code.c_str(), false);
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: str (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: str\n", errout.str());
}
void switch3() {
@ -1303,7 +1303,7 @@ private:
" }\n"
" delete [] str;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: str (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: str\n", errout.str());
}
void switch4() {
@ -1456,7 +1456,7 @@ private:
" char *p = new char[100];\n"
" foo(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str());
}
@ -1502,7 +1502,7 @@ private:
" char *p = new char[100];\n"
" foo(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: p\n", errout.str());
}
@ -1519,7 +1519,7 @@ private:
" char *p = new char[100];\n"
" foo(p);\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p (this might be a false warning)\n",
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n",
"", errout.str());
}
@ -1612,7 +1612,7 @@ private:
" if (a()) return;\n" // <- memory leak
" free(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str());
}
void func16() {
@ -1752,7 +1752,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1766,7 +1766,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1797,7 +1797,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1811,7 +1811,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1843,7 +1843,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1857,7 +1857,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile\n", errout.str());
check("void foo ()\n"
"{\n"
@ -1890,7 +1890,7 @@ private:
" delete [] cpDir;\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -1903,7 +1903,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -1934,7 +1934,7 @@ private:
" delete [] cpDir;\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -1947,7 +1947,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -1979,7 +1979,7 @@ private:
" delete [] cpDir;\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -1992,7 +1992,7 @@ private:
" }\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: cpDir\n", errout.str());
check("void foo()\n"
"{\n"
@ -2027,7 +2027,7 @@ private:
" sprintf(cBuf,\"%s\",\"testtest..\");\n"
" perror (cBuf);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: cBuf (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: cBuf\n", errout.str());
}
// # 2668
@ -2044,7 +2044,7 @@ private:
" delete [] cpFile;\n"
" fclose (stdout);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile\n", errout.str());
check("void foo()\n"
"{\n"
@ -2058,7 +2058,7 @@ private:
" }\n"
" fclose (stdout);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: cpFile\n", errout.str());
check("void foo()\n"
"{\n"
@ -2093,7 +2093,7 @@ private:
" delete [] cpFile;\n"
" return file;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile\n", errout.str());
check("int * foo()\n"
"{\n"
@ -2107,7 +2107,7 @@ private:
" }\n"
" return file;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile\n", errout.str());
check("int * foo()\n"
"{\n"
@ -2139,7 +2139,7 @@ private:
" delete [] cpFile;\n"
" return file;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: cpFile\n", errout.str());
check("int * foo()\n"
"{\n"
@ -2153,7 +2153,7 @@ private:
" }\n"
" return file;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: cpFile\n", errout.str());
check("int * foo()\n"
"{\n"
@ -2177,7 +2177,7 @@ private:
"{\n"
" std::string *x = new std::string;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x (this might be a false warning)\n","", errout.str());
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: x\n","", errout.str());
check("void f(void) \n"
"{\n"
@ -2201,7 +2201,7 @@ private:
"{\n"
" Fred *f = new Fred();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: f\n", errout.str());
check("class Fred { void foo(){ } };\n"
"void f(void) \n"
@ -2216,7 +2216,7 @@ private:
"{\n"
" Fred *f = new Fred();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: f\n", errout.str());
}
@ -2229,7 +2229,7 @@ private:
"{\n"
" char *p = a();\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str());
check("FILE *a()\n"
"{\n"
@ -2239,7 +2239,7 @@ private:
"{\n"
" FILE *p = a();\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Resource leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Resource leak: p\n"), errout.str());
check("char *a()\n"
"{\n"
@ -2249,7 +2249,7 @@ private:
"{\n"
" char *p = a();\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str());
}
void allocfunc2() {
@ -2302,7 +2302,7 @@ private:
"{\n"
" char *p = a();\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str());
}
void allocfunc4() {
@ -2317,7 +2317,7 @@ private:
"{\n"
" char *p = foo();\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p\n"), errout.str());
check("char* foo()\n"
"{\n"
@ -2345,7 +2345,7 @@ private:
" char *p;\n"
" foo(&p);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:10]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:10]: (error) Memory leak: p\n"), errout.str());
check("void foo(char **str)\n"
"{\n"
@ -2371,7 +2371,7 @@ private:
" char *q;\n"
" foo(&q, &p);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p (this might be a false warning)\n"), errout.str());
ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p\n"), errout.str());
check("void foo(char **str)\n"
"{\n"
@ -2384,7 +2384,7 @@ private:
" char *p;\n"
" foo(&p);\n"
"}\n");
TODO_ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p (this might be a false warning)\n"),
TODO_ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p\n"),
"", errout.str());
check("void foo(char **str)\n"
@ -2469,7 +2469,7 @@ private:
" f = foo();\n"
" fclose(f);\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Resource leak: f\n", errout.str());
}
void allocfunc9() {
@ -2508,7 +2508,7 @@ private:
" delete [] str;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: str\n", errout.str());
}
void throw2() {
@ -2570,7 +2570,7 @@ private:
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: s2 (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:12]: (error) Memory leak: s2\n", errout.str());
}
@ -2581,7 +2581,7 @@ private:
" a = realloc(a, 100);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n"
"[test.cpp:5]: (error) Memory leak: a (this might be a false warning)\n", errout.str());
"[test.cpp:5]: (error) Memory leak: a\n", errout.str());
}
void realloc2() {
@ -2748,7 +2748,7 @@ private:
" free(a);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
check("void foo()\n"
"{\n"
@ -2825,7 +2825,7 @@ private:
" char *a = reinterpret_cast<char *>(malloc(10));\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: a\n", errout.str());
}
@ -3022,7 +3022,7 @@ private:
" ThrowException();\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: p\n", errout.str());
check("void foo()\n"
"{\n"
@ -3030,7 +3030,7 @@ private:
" p = g();\n"
" delete [] p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
}
void unknownFunction4() {
@ -3041,7 +3041,7 @@ private:
" if (b) return;\n"
" delete [] p;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
}
void unknownFunction5() {
@ -3091,7 +3091,7 @@ private:
" int *p = new int[100];\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:7]: (error) Memory leak: p\n", errout.str());
}
void class2() {
@ -3191,7 +3191,7 @@ private:
" exit(0);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str());
}
void exit5() {
@ -3237,7 +3237,7 @@ private:
" char *p = malloc(100);\n"
" int i = a(123);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n", errout.str());
}
void noreturn() {
@ -3259,7 +3259,7 @@ private:
" char *p = malloc(100);\n"
" fatal_error();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:8]: (error) Memory leak: p\n", errout.str());
}
@ -3271,7 +3271,7 @@ private:
" memset(&(out[0]), 0, 1);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: out (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: out\n", errout.str());
}
void strndup_function() {
@ -3279,7 +3279,7 @@ private:
"{\n"
" char *out = strndup(\"text\", 3);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: out (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: out\n", errout.str());
}
void tmpfile_function() {
@ -3287,7 +3287,7 @@ private:
"{\n"
" FILE *f = tmpfile();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
check("void f()\n"
"{\n"
@ -3295,7 +3295,7 @@ private:
" if (!f)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: f\n", errout.str());
check("void f()\n"
"{\n"
@ -3341,7 +3341,7 @@ private:
"{\n"
" int fd = open(path, O_RDONLY);\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
check("void f(const char *path)\n"
"{\n"
@ -3387,7 +3387,7 @@ private:
"{\n"
" int fd = creat(path, S_IRWXU);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: fd\n", errout.str());
}
void close_function() {
@ -3447,7 +3447,7 @@ private:
" }\n"
" close(handle);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:11]: (error) Resource leak: handle (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:11]: (error) Resource leak: handle\n", errout.str());
}
void fd_functions() {
@ -3475,7 +3475,7 @@ private:
" fstat(fd, buf);\n"
" fchmod(fd, mode);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:24]: (error) Resource leak: fd (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:24]: (error) Resource leak: fd\n", errout.str());
}
void opendir_function() {
@ -3483,7 +3483,7 @@ private:
"{\n"
" DIR *f = opendir(\".\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
}
void fdopendir_function() {
@ -3491,7 +3491,7 @@ private:
"{\n"
" DIR *f = fdopendir(fd);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
}
void closedir_function() {
@ -3528,7 +3528,7 @@ private:
" seekdir(f, 2)\n;"
" scandir(f, namelist, filter, comp);\n;"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Resource leak: f\n", errout.str());
}
void file_functions() {
@ -3554,7 +3554,7 @@ private:
"fgetpos(in, 10);\n"
"fprintf(in, \"text\\n\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:22]: (error) Resource leak: f (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:22]: (error) Resource leak: f\n", errout.str());
}
void getc_function() {
@ -3566,7 +3566,7 @@ private:
" while ( (c = getc (fin1a)) != EOF)\n"
" { }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: fin1a (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: fin1a\n", errout.str());
}
{
@ -3576,7 +3576,7 @@ private:
" FILE *fin1b = fopen(\"FILE.txt\", \"r\");\n"
" c = getc(fin1b);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: fin1b (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: fin1b\n", errout.str());
}
}
@ -3632,7 +3632,7 @@ private:
" int *p = new int[100];\n"
" typeid(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: p\n", errout.str());
}
void same_function_name() {
@ -3651,7 +3651,7 @@ private:
"{\n"
" p = malloc(100);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n", errout.str());
}
// Ticket #2014 - setjmp / longjmp
@ -3690,7 +3690,7 @@ private:
"return 0;\n"
"}\n"
);
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: buff (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: buff\n", errout.str());
}
void trac2662() {
@ -3736,7 +3736,7 @@ private:
"try {}\n"
"catch(...) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a (this might be a false warning)\n", errout.str());
ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str());
}

View File

@ -84,7 +84,7 @@ private:
oss << "int main()\n"
<< "{\n";
for (int i = 0; i < 500; i++)
oss << " { int a = 4/0; }\n";
oss << " {char *a = malloc(10);}\n";
oss << " return 0;\n";
oss << "}\n";
@ -122,7 +122,7 @@ private:
std::ostringstream oss;
oss << "int main()\n"
<< "{\n";
oss << " { int a = 4/0; }\n";
oss << " {char *a = malloc(10);}\n";
oss << " return 0;\n";
oss << "}\n";
check(2, 1, 1, oss.str());
@ -132,7 +132,7 @@ private:
std::ostringstream oss;
oss << "int main()\n"
<< "{\n";
oss << " { int a = 4/0; }\n";
oss << " {char *a = malloc(10);}\n";
oss << " return 0;\n";
oss << "}\n";
check(2, 20, 20, oss.str());