fix #2926 (false negative in auto variables checking when function parameter is a struct)

This commit is contained in:
Robert Reif 2011-07-21 22:26:42 -04:00
parent fd40e6a362
commit 4e64d35462
2 changed files with 21 additions and 0 deletions

View File

@ -101,6 +101,16 @@ void CheckAutoVariables::autoVariables()
if (var && (!var->isClass() || var->type()))
errorAutoVariableAssignment(tok);
}
else if (Token::Match(tok, "[;{}] %var% . %var% = & %var%"))
{
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId());
if (var1 && var1->isArgument() && Token::Match(var1->nameToken()->tokAt(-2), "%type% *"))
{
const Variable * var2 = symbolDatabase->getVariableFromVarId(tok->tokAt(6)->varId());
if (var2 && var2->isLocal() && !var2->isStatic())
errorAutoVariableAssignment(tok);
}
}
else if (Token::Match(tok, "[;{}] * %var% = %var% ;"))
{
const Variable * var1 = symbolDatabase->getVariableFromVarId(tok->tokAt(2)->varId());

View File

@ -71,6 +71,7 @@ private:
TEST_CASE(testautovar2);
TEST_CASE(testautovar3); // ticket #2925
TEST_CASE(testautovar4); // ticket #2928
TEST_CASE(testautovar5); // ticket #2926
TEST_CASE(testautovar_array1);
TEST_CASE(testautovar_array2);
TEST_CASE(testautovar_return1);
@ -175,6 +176,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void testautovar5() // ticket #2926
{
check("void foo(struct AB *ab)\n"
"{\n"
" char a;\n"
" ab->a = &a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Assigning address of local auto-variable to a function parameter.\n", errout.str());
}
void testautovar_array1()
{
check("void func1(int* arr[2])\n"