Fixed #590 (False positive: Invalid deallocation when variables have same name)

http://sourceforge.net/apps/trac/cppcheck/ticket/590
This commit is contained in:
Slava Semushin 2009-08-16 15:27:40 +07:00
parent e2681be8f2
commit be89be8c30
3 changed files with 18 additions and 11 deletions

View File

@ -92,14 +92,12 @@ bool CheckAutoVariables::isAutoVar(const Token* t)
return false;
}
bool CheckAutoVariables::isAutoVarArray(const Token* t)
bool CheckAutoVariables::isAutoVarArray(unsigned int varId)
{
std::list<std::string>::iterator id_vd;
std::string v(t->str());
std::list<unsigned int>::iterator id_vd;
for (id_vd = vda_list.begin(); id_vd != vda_list.end(); ++id_vd)
{
std::string vname(*id_vd);
if (vname == v)
if (*id_vd == varId)
return true;
}
return false;
@ -153,9 +151,7 @@ void CheckAutoVariables::addVD(const Token* tok)
void CheckAutoVariables::addVDA(const Token* tok)
{
std::string var_name(tok->str());
vda_list.push_back(var_name);
vda_list.push_back(tok->varId());
}
void CheckAutoVariables::autoVariables()
@ -244,7 +240,7 @@ void CheckAutoVariables::autoVariables()
// Invalid pointer deallocation
else if (bindent > 0 && Token::Match(tok, "free ( %var% ) ;"))
{
if (isAutoVarArray(tok->tokAt(2)))
if (isAutoVarArray(tok->tokAt(2)->varId()))
reportError(tok,
Severity::error,
"autoVariables",

View File

@ -58,10 +58,10 @@ public:
private:
std::list<std::string> fp_list;
std::list<std::string> vd_list;
std::list<std::string> vda_list;
std::list<unsigned int> vda_list;
bool errorAv(const Token* left, const Token* right);
bool isAutoVar(const Token* t);
bool isAutoVarArray(const Token* t);
bool isAutoVarArray(unsigned int varId);
void addVD(const Token* t);
void addVDA(const Token* t);

View File

@ -126,6 +126,17 @@ private:
"free (tmp);\n"
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:5]: (error) Invalid deallocation\n"), errout.str());
check("void f()\n"
"{\n"
" char psz_title[10];\n"
" {\n"
" char *psz_title = 0;\n"
" abc(0, psz_title);\n"
" free(psz_title);\n"
" }\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
void returnLocalVariable1()