From 1b9369b78b88243436b0940b4bec076884f21b92 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 23 Apr 2023 07:38:28 -0500 Subject: [PATCH] Fix 11666: FP returnDanglingLifetime with specializations and ptr to member (#5003) * Fix 11666: FP returnDanglingLifetime with specializations and ptr to member * Format --- lib/valueflow.cpp | 13 +++++++++---- test/testautovariables.cpp | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index c2180ad9e..31223e7c0 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4519,10 +4519,15 @@ static void valueFlowLifetimeClassConstructor(Token* tok, std::vector args = getArguments(tok); if (scope->numConstructors == 0) { auto it = scope->varlist.cbegin(); - LifetimeStore::forEach(args, - "Passed to constructor of '" + t->name() + "'.", - ValueFlow::Value::LifetimeKind::SubObject, - [&](const LifetimeStore& ls) { + LifetimeStore::forEach( + args, + "Passed to constructor of '" + t->name() + "'.", + ValueFlow::Value::LifetimeKind::SubObject, + [&](const LifetimeStore& ls) { + // Skip static variable + it = std::find_if(it, scope->varlist.cend(), [](const Variable& var) { + return !var.isStatic(); + }); if (it == scope->varlist.cend()) return; const Variable& var = *it; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 69495e517..fadc184f5 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3235,6 +3235,28 @@ private: " return &a[0].x;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #11666 + check("template \n" + "struct Matrix;\n" + "template \n" + "struct Matrix {\n" + " std::array x;\n" + "private:\n" + " static constexpr decltype(&Matrix::x) members[] = {&Matrix::x};\n" + "};\n" + "template \n" + "struct Matrix {\n" + " std::array x;\n" + " std::array y;\n" + "private:\n" + " static constexpr decltype(&Matrix::x) members[] = {&Matrix::x, &Matrix::y};\n" + "};\n" + "template \n" + "Matrix O() {\n" + " return { {}, {} };\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeFunction() {