Fixed #2975 (False positive: Return of the address of an auto-variable)

This commit is contained in:
Daniel Marjamäki 2011-08-10 18:16:31 +02:00
parent a30da73d3e
commit 9a24492a00
3 changed files with 37 additions and 14 deletions

View File

@ -56,6 +56,14 @@ bool CheckAutoVariables::isAutoVar(unsigned int varId)
if (!var || !var->isLocal() || var->isStatic() || var->isArray() || var->typeEndToken()->str() == "*") if (!var || !var->isLocal() || var->isStatic() || var->isArray() || var->typeEndToken()->str() == "*")
return false; return false;
if (Token::simpleMatch(var->nameToken()->previous(), "&"))
{
// address of reference variable can be taken if the address
// of the variable it points at is not a auto-var
// TODO: check what the reference variable references.
return false;
}
return true; return true;
} }

View File

@ -2082,20 +2082,23 @@ void Tokenizer::simplifyMulAnd(void)
{ {
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
//fix Ticket #2784 if (Token::Match(tok, "[;{}] *"))
if (Token::Match(tok->next(), "* & %any% ="))
{ {
tok->deleteNext(); //del * //fix Ticket #2784
tok->deleteNext(); //del & if (Token::Match(tok->next(), "* & %any% ="))
continue; {
} tok->deleteNext(); //del *
if (Token::Match(tok->next(), "* ( & %any% ) =")) tok->deleteNext(); //del &
{ continue;
tok->deleteNext(); //del * }
tok->deleteNext(); //del ( if (Token::Match(tok->next(), "* ( & %any% ) ="))
tok->deleteNext(); //del & {
tok->next()->deleteNext(); //del ) tok->deleteNext(); //del *
continue; tok->deleteNext(); //del (
tok->deleteNext(); //del &
tok->next()->deleteNext(); //del )
continue;
}
} }
} }
} }
@ -2115,7 +2118,7 @@ bool Tokenizer::tokenize(std::istream &code,
createTokens(code); createTokens(code);
// simplify '* & %any% =' to '%any% =' // simplify '[;{}] * & %any% =' to '%any% ='
simplifyMulAnd(); simplifyMulAnd();
// Convert C# code // Convert C# code

View File

@ -77,6 +77,7 @@ private:
TEST_CASE(testautovar_array2); TEST_CASE(testautovar_array2);
TEST_CASE(testautovar_return1); TEST_CASE(testautovar_return1);
TEST_CASE(testautovar_return2); TEST_CASE(testautovar_return2);
TEST_CASE(testautovar_return3);
TEST_CASE(testautovar_extern); TEST_CASE(testautovar_extern);
TEST_CASE(testinvaliddealloc); TEST_CASE(testinvaliddealloc);
TEST_CASE(testassign1); // Ticket #1819 TEST_CASE(testassign1); // Ticket #1819
@ -257,6 +258,17 @@ private:
ASSERT_EQUALS("[test.cpp:6]: (error) Return of the address of an auto-variable\n", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Return of the address of an auto-variable\n", errout.str());
} }
void testautovar_return3()
{
// #2975 - FP
check("void** f()\n"
"{\n"
" void *&value = tls[id];"
" return &value;"
"}");
ASSERT_EQUALS("", errout.str());
}
void testautovar_extern() void testautovar_extern()
{ {
check("struct foo *f()\n" check("struct foo *f()\n"