Fixed ticket #517 (False positive for extern variables: return of the address of an auto-variable)

http://sourceforge.net/apps/trac/cppcheck/ticket/517
This commit is contained in:
Slava Semushin 2009-07-28 00:32:01 +07:00
parent 65d6bce165
commit bb4b2b707c
2 changed files with 18 additions and 7 deletions

View File

@ -110,15 +110,15 @@ bool isTypeName(const Token *tok)
ret |= (_str == type[i]);
return !ret;
}
bool isStatic(const Token *tok)
bool isExternOrStatic(const Token *tok)
{
bool res = false;
if (tok->tokAt(-1)->str() == "static")
if (Token::Match(tok->tokAt(-1), "extern|static"))
res = true;
else if (tok->tokAt(-2)->str() == "static")
else if (Token::Match(tok->tokAt(-2), "extern|static"))
res = true;
else if (tok->tokAt(-3)->str() == "static")
else if (Token::Match(tok->tokAt(-3), "extern|static"))
res = true;
//std::cout << __PRETTY_FUNCTION__ << " " << tok->str() << " " << res << std::endl;
@ -169,17 +169,17 @@ void CheckAutoVariables::autoVariables()
{
bindent--;
}
else if (bindent > 0 && Token::Match(tok, "%type% :: %any%") && !isStatic(tok)) //Inside a function
else if (bindent > 0 && Token::Match(tok, "%type% :: %any%") && !isExternOrStatic(tok)) //Inside a function
{
addVD(tok->tokAt(2));
}
else if (bindent > 0 && Token::Match(tok, "%var% %var% ;") && !isStatic(tok)) //Inside a function
else if (bindent > 0 && Token::Match(tok, "%var% %var% ;") && !isExternOrStatic(tok)) //Inside a function
{
if (!isTypeName(tok))
continue;
addVD(tok->tokAt(1));
}
else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;") && !isStatic(tok)) //Inside a function
else if (bindent > 0 && Token::Match(tok, "const %var% %var% ;") && !isExternOrStatic(tok)) //Inside a function
{
if (!isTypeName(tok->tokAt(1)))
continue;

View File

@ -64,6 +64,7 @@ private:
TEST_CASE(testautovar);
TEST_CASE(testautovararray);
TEST_CASE(testautovarreturn);
TEST_CASE(testautovar_extern);
TEST_CASE(returnLocalVariable1);
TEST_CASE(returnLocalVariable2);
@ -103,6 +104,16 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Return of the address of an auto-variable\n", errout.str());
}
void testautovar_extern()
{
check("struct foo *f()\n"
"{\n"
" extern struct foo f;\n"
" return &f;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnLocalVariable1()
{