memory leak: fixed ticket #9

This commit is contained in:
Daniel Marjamäki 2009-02-04 06:11:36 +00:00
parent 142a21973a
commit c46c4c50e9
3 changed files with 32 additions and 8 deletions

View File

@ -308,7 +308,7 @@ bool CheckMemoryLeakClass::MatchFunctionsThatReturnArg(const Token *tok, const s
return Token::Match(tok, std::string("; " + varname + " = strcat|memcpy|memmove|strcpy ( " + varname + " ,").c_str());
}
bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[])
bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[], bool endpar)
{
std::string varname;
for (int i = 0; varnames[i]; i++)
@ -319,10 +319,12 @@ bool CheckMemoryLeakClass::notvar(const Token *tok, const char *varnames[])
varname += varnames[i];
}
return bool(Token::Match(tok, std::string("! " + varname + " [;)&|]").c_str()) ||
Token::simpleMatch(tok, std::string("! ( " + varname + " )").c_str()) ||
Token::Match(tok, std::string("0 == " + varname + " [;)&|]").c_str()) ||
Token::simpleMatch(tok, std::string(varname + " == 0").c_str()));
const std::string end(endpar ? " )" : " [;)&|]");
return bool(Token::Match(tok, std::string("! " + varname + end).c_str()) ||
Token::simpleMatch(tok, std::string("! ( " + varname + " )" + end).c_str()) ||
Token::Match(tok, std::string("0 == " + varname + end).c_str()) ||
Token::simpleMatch(tok, std::string(varname + " == 0" + end).c_str()));
}
Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype, bool classmember, bool &all)
@ -485,7 +487,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
while (tok->str() != ")")
tok = tok->next();
}
else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varnames))
else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varnames, true))
{
addtoken("if(!var)");
}

View File

@ -89,7 +89,16 @@ private:
* memory by calling Tokenizer::deleteTokens(returnValue);
*/
Token *getcode(const Token *tok, std::list<const Token *> callstack, const char varname[], AllocType &alloctype, AllocType &dealloctype, bool classmember, bool &all);
bool notvar(const Token *tok, const char *varnames[]);
/**
* Check if there is a "!var" match inside a condition
* @param tok first token to match
* @param varnames the varname
* @param endpar if this is true the "!var" must be followed by ")"
* @return true if match
*/
bool notvar(const Token *tok, const char *varnames[], bool endpar = false);
bool MatchFunctionsThatReturnArg(const Token *tok, const std::string &varname);
void MemoryLeak(const Token *tok, const char varname[], AllocType alloctype, bool all);
void MismatchError(const Token *Tok1, const std::list<const Token *> &callstack, const char varname[]);

View File

@ -94,6 +94,7 @@ private:
TEST_CASE(if8); // Bug 2458532
// TODO TEST_CASE( if9 ); // if (realloc)
TEST_CASE(if10); // else if (realloc)
TEST_CASE(if11);
TEST_CASE(forwhile1);
TEST_CASE(forwhile2);
@ -629,7 +630,19 @@ private:
ASSERT_EQUALS(std::string(""), errout.str());
}
void if11()
{
check("void foo()\n"
"{\n"
" int *x = new int[10];\n"
" if (x == 0 || aa)\n"
" {\n"
" return 1;\n"
" }\n"
" delete [] x;\n"
"}\n", true);
ASSERT_EQUALS(std::string("[test.cpp:6]: (always) Memory leak: x\n"), errout.str());
}