diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index b6f6ca7ac..c93bad510 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -55,10 +55,10 @@ static bool isPtrArg(const Token *tok) return (var && var->isArgument() && var->isPointer()); } -static bool isArrayArg(const Token *tok) +static bool isArrayArg(const Token *tok, const Settings* settings) { const Variable *var = tok->variable(); - return (var && var->isArgument() && var->isArray()); + return (var && var->isArgument() && var->isArray() && !settings->library.isentrypoint(var->scope()->className)); } static bool isArrayVar(const Token *tok) @@ -266,7 +266,7 @@ void CheckAutoVariables::autoVariables() } tok = tok->tokAt(4); } else if (Token::Match(tok, "[;{}] %var% [") && Token::simpleMatch(tok->linkAt(2), "] =") && - (isPtrArg(tok->next()) || isArrayArg(tok->next())) && isAddressOfLocalVariable(tok->linkAt(2)->next()->astOperand2())) { + (isPtrArg(tok->next()) || isArrayArg(tok->next(), mSettings)) && isAddressOfLocalVariable(tok->linkAt(2)->next()->astOperand2())) { errorAutoVariableAssignment(tok->next(), false); } // Invalid pointer deallocation diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 5b9a6e0a6..ea81c6989 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -68,6 +68,7 @@ private: TEST_CASE(testautovar16); // ticket #8114 TEST_CASE(testautovar_array1); TEST_CASE(testautovar_array2); + TEST_CASE(testautovar_array3); TEST_CASE(testautovar_normal); // "normal" token list that does not remove casts etc TEST_CASE(testautovar_ptrptr); // ticket #6956 TEST_CASE(testautovar_return1); @@ -517,6 +518,14 @@ private: ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); } + void testautovar_array3() { + check("int main(int argc, char* argv[]) {\n" // #11732 + " std::string a = \"abc\";\n" + " argv[0] = &a[0];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void testautovar_normal() { check("void f(XmDestinationCallbackStruct *ds)\n" "{\n"