Fix #368 (Leak detected when allocated memory assigned to member of structure which returned from function)

The return value of strcpy wasn't handled very well
This commit is contained in:
Daniel Marjamäki 2009-06-06 20:55:16 +02:00
parent acaa22ff6c
commit a8c5526c84
2 changed files with 19 additions and 7 deletions

View File

@ -595,25 +595,25 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
addtoken(tok->str().c_str());
}
if ((tok->str() == "case"))
else if ((tok->str() == "case"))
{
addtoken("case");
addtoken(";");
}
if ((tok->str() == "default"))
else if ((tok->str() == "default"))
{
addtoken("default");
addtoken(";");
}
// Loops..
if ((tok->str() == "for") || (tok->str() == "while"))
else if ((tok->str() == "for") || (tok->str() == "while"))
{
addtoken("loop");
isloop = true;
}
if ((tok->str() == "do"))
else if ((tok->str() == "do"))
{
addtoken("do");
}
@ -633,7 +633,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
}
// Return..
if (tok->str() == "return")
else if (tok->str() == "return")
{
addtoken("return");
@ -684,7 +684,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
}
// throw..
if (Token::Match(tok, "try|throw|catch"))
else if (Token::Match(tok, "try|throw|catch"))
addtoken(tok->strAt(0));
// exit..
@ -694,7 +694,8 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list<const Token *>
// Assignment..
if (Token::Match(tok, std::string("[)=] " + varnameStr + " [+;)]").c_str()) ||
Token::Match(tok, std::string(varnameStr + " +=|-=").c_str()) ||
Token::Match(tok, std::string("+=|<< " + varnameStr + " ;").c_str()))
Token::Match(tok, std::string("+=|<< " + varnameStr + " ;").c_str()) ||
Token::Match(tok, std::string("= strcpy|strcat|memmove|memcpy ( " + varnameStr + " ,").c_str()))
{
addtoken("use");
}

View File

@ -190,6 +190,7 @@ private:
// free a free'd pointer
TEST_CASE(freefree1);
TEST_CASE(freefree2);
TEST_CASE(strcpy_result_assignment);
TEST_CASE(strcat_result_assignment);
TEST_CASE(all1); // Extra checking when --all is given
@ -1974,6 +1975,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void strcpy_result_assignment()
{
check("void foo()\n"
"{\n"
" char *p1 = malloc(10);\n"
" char *p2 = strcpy(p1, \"a\");\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void strcat_result_assignment()
{
check("void foo()\n"