From 32f94a47cc87df7e174f2d8919783e5ac2be8c2f Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Wed, 20 Jul 2011 19:02:54 -0400 Subject: [PATCH] fix #2925 (False negative: auto variables - assigning address of array to function parameter) --- lib/checkautovariables.cpp | 11 +++++++++++ test/testautovariables.cpp | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index da305c699..164a94195 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -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); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index afa2bb5c4..8d81b6e2f 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -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"