From cdc867cb01ecd50a244bd98280c3b8fa934a040c Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 9 May 2023 16:19:24 +0200 Subject: [PATCH 1/4] Fix #10976 false negative: autoVariables [inconclusive] (regression) --- 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 ce7a72038bd..5eb6c45b7cd 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->str() == "]") + lhs = lhs->link()->previous(); + 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 9f2dd2105ef..5b9a6e0a6e6 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -617,6 +617,18 @@ class TestAutoVariables : public TestFixture { " 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() { From b3f0340b0aa3287d777ee7de3dc9eb377b0b67fa Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 9 May 2023 16:21:09 +0200 Subject: [PATCH 2/4] Use link() --- lib/checkautovariables.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 5eb6c45b7cd..013f014bdc5 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -310,7 +310,7 @@ bool CheckAutoVariables::checkAutoVariableAssignment(const Token *expr, bool inc if (Token::simpleMatch(tok, "=")) { const Token *lhs = tok; while (Token::Match(lhs->previous(), "%name%|.|*|]")) { - if (lhs->str() == "]") + if (lhs->link()) lhs = lhs->link()->previous(); else lhs = lhs->previous(); From 29b43bec11a20e6ccdce0e4baccfb16c59ed5e24 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 9 May 2023 16:52:46 +0200 Subject: [PATCH 3/4] Use linkAt() --- lib/checkautovariables.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 013f014bdc5..11d5144fc8a 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -310,8 +310,8 @@ bool CheckAutoVariables::checkAutoVariableAssignment(const Token *expr, bool inc if (Token::simpleMatch(tok, "=")) { const Token *lhs = tok; while (Token::Match(lhs->previous(), "%name%|.|*|]")) { - if (lhs->link()) - lhs = lhs->link()->previous(); + if (lhs->linkAt(-1)) + lhs = lhs->linkAt(-1)->previous(); else lhs = lhs->previous(); } From 286e3378d146cae567a3dc232c33a21d1228b3bf Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 9 May 2023 18:33:06 +0200 Subject: [PATCH 4/4] Skip over [][] --- lib/checkautovariables.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 11d5144fc8a..1cc494b88c8 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -311,7 +311,7 @@ bool CheckAutoVariables::checkAutoVariableAssignment(const Token *expr, bool inc const Token *lhs = tok; while (Token::Match(lhs->previous(), "%name%|.|*|]")) { if (lhs->linkAt(-1)) - lhs = lhs->linkAt(-1)->previous(); + lhs = lhs->linkAt(-1); else lhs = lhs->previous(); }