From 4ef20f8f1e2232728e6430c9156d0dacb661ab42 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 10 Jan 2022 00:34:26 -0600 Subject: [PATCH] Fix 10683: FP danglingTemporaryLifetime with pointer to vector (#3685) --- lib/valueflow.cpp | 6 ++---- test/testautovariables.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 8a9416415..d1b76d118 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4200,11 +4200,9 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger std::vector toks = {}; if (tok->isUnaryOp("*") || parent->originalName() == "->") { for (const ValueFlow::Value& v : tok->values()) { - if (!v.isSymbolicValue()) + if (!v.isLocalLifetimeValue()) continue; - if (v.isKnown()) - continue; - if (v.intvalue != 0) + if (v.lifetimeKind != ValueFlow::Value::LifetimeKind::Address) continue; if (!v.tokvalue) continue; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 5d362eb76..a25e935b0 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -3295,6 +3295,16 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:9] -> [test.cpp:10]: (error) Using iterator that is a temporary.\n", errout.str()); + + check("void f(bool b) {\n" + " std::vector ints = g();\n" + " auto *ptr = &ints;\n" + " if (b)\n" + " ptr = &ints;\n" + " for (auto it = ptr->begin(); it != ptr->end(); ++it)\n" + " {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void danglingLifetimeBorrowedMembers()