From 229c45e7f887a05d763d7eaa61d7d7045d8dcd05 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sat, 1 Dec 2018 19:07:46 +0100 Subject: [PATCH] Fix issue 8865: FP with dangling lifetime This fixes: ```cpp void f(uint32_t event, unsigned long op, const xen_ulong_t *args) { struct __packed { uint32_t op; uint32_t args[6]; } d; uint32_t *a = d.args; } ``` --- lib/checkautovariables.cpp | 3 ++- test/testautovariables.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 675781635..93c0f626d 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -643,7 +643,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token } else if (isDeadScope(val.tokvalue, tok->scope())) { errorInvalidLifetime(tok, &val); break; - } else if (tok->variable() && !tok->variable()->isLocal() && !tok->variable()->isArgument() && + } else if (tok->variable() && tok->variable()->declarationId() == tok->varId() && + !tok->variable()->isLocal() && !tok->variable()->isArgument() && isInScope(val.tokvalue, tok->scope())) { errorDanglngLifetime(tok, &val); break; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index e717596aa..3629bdcaa 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1510,6 +1510,14 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("void f() {\n" + " struct b {\n" + " uint32_t f[6];\n" + " } d;\n" + " uint32_t *a = d.f;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // Make sure we dont hang check("struct A;\n" "void f() {\n"