partial fix for #3030 (Improve check: Returning pointer to local array variable with '&' pointer operator)

This commit is contained in:
Robert Reif 2011-08-20 15:08:30 -04:00
parent d5bf5b7ad2
commit 7eb43bc107
2 changed files with 18 additions and 0 deletions

View File

@ -159,6 +159,12 @@ void CheckAutoVariables::autoVariables()
{
errorReturnAddressToAutoVariable(tok);
}
else if (Token::Match(tok, "return & %var% [") &&
Token::Match(tok->tokAt(3)->link(), "] ;") &&
isAutoVarArray(tok->tokAt(2)->varId()))
{
errorReturnAddressToAutoVariable(tok);
}
// Invalid pointer deallocation
else if (Token::Match(tok, "free ( %var% ) ;") && isAutoVarArray(tok->tokAt(2)->varId()))
{

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(testautovar_return1);
TEST_CASE(testautovar_return2);
TEST_CASE(testautovar_return3);
TEST_CASE(testautovar_return4); // ticket #3030
TEST_CASE(testautovar_extern);
TEST_CASE(testinvaliddealloc);
TEST_CASE(testassign1); // Ticket #1819
@ -269,6 +270,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void testautovar_return4()
{
// #3030
check("char *foo()\n"
"{\n"
" char q[] = \"AAAAAAAAAAAA\";\n"
" return &q[1];\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Return of the address of an auto-variable\n", errout.str());
}
void testautovar_extern()
{
check("struct foo *f()\n"