From 6a8c70c1b9f0067f360800758317564308934e00 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 13 May 2023 14:11:01 +0200 Subject: [PATCH] Fix #10976 false negative: autoVariables [inconclusive] (regression) (#5044) * Fix #10976 false negative: autoVariables [inconclusive] (regression) * Use link() * Use linkAt() * Skip over [][] --- lib/checkautovariables.cpp | 8 ++++++-- test/testautovariables.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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() {