diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index ce7a72038..1cc494b88 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -309,8 +309,12 @@ bool CheckAutoVariables::checkAutoVariableAssignment(const Token *expr, bool inc } if (Token::simpleMatch(tok, "=")) { const Token *lhs = tok; - while (Token::Match(lhs->previous(), "%name%|.|*")) - lhs = lhs->previous(); + while (Token::Match(lhs->previous(), "%name%|.|*|]")) { + if (lhs->linkAt(-1)) + lhs = lhs->linkAt(-1); + else + lhs = lhs->previous(); + } const Token *e = expr; while (e->str() != "=" && lhs->str() == e->str()) { e = e->next(); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 9f2dd2105..5b9a6e0a6 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -617,6 +617,18 @@ private: " pcb->root0 = 0;\n" // <- conditional reassign => error "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); + + check("struct S { int *p; };\n" + "void g(struct S* s) {\n" + " int a[10];\n" + " s->p = a;\n" + " a[0] = 0;\n" + "}\n" + "void f() {\n" + " struct S s;\n" + " g(&s);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (error, inconclusive) Address of local auto-variable assigned to a function parameter.\n", errout.str()); } void testinvaliddealloc() {