fix #2925 (False negative: auto variables - assigning address of array to function parameter)

This commit is contained in:
Robert Reif 2011-07-20 19:02:54 -04:00
parent 7e7f6eecf5
commit 32f94a47cc
2 changed files with 22 additions and 0 deletions

View File

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

View File

@ -69,6 +69,7 @@ private:
{
TEST_CASE(testautovar1);
TEST_CASE(testautovar2);
TEST_CASE(testautovar3); // ticket #2925
TEST_CASE(testautovar_array1);
TEST_CASE(testautovar_array2);
TEST_CASE(testautovar_return1);
@ -153,6 +154,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void testautovar3() // ticket #2925
{
check("void foo(int **p)\n"
"{\n"
" int x[100];\n"
" *p = x;\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"