From e2efb338b6ad80e041a7f7bb97f86ad3aeb00702 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Sun, 19 Apr 2020 01:28:07 -0500 Subject: [PATCH] Fix issue 9678: False positive: generic valueflow forward analysis (#2611) --- lib/valueflow.cpp | 6 +++++- test/testvalueflow.cpp | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index f29406d44..609d53dbd 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3714,7 +3714,11 @@ static void valueFlowForwardAssign(Token * const tok, ErrorLogger * const errorLogger, const Settings * const settings) { - const Token * const endOfVarScope = var->scope()->bodyEnd; + const Token * endOfVarScope = nullptr; + if (var->isLocal()) + endOfVarScope = var->scope()->bodyEnd; + if (!endOfVarScope) + endOfVarScope = tok->scope()->bodyEnd; if (std::any_of(values.begin(), values.end(), std::mem_fn(&ValueFlow::Value::isLifetimeValue))) { valueFlowForwardLifetime(tok, tokenlist, errorLogger, settings); values.remove_if(std::mem_fn(&ValueFlow::Value::isLifetimeValue)); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 23d1dde0b..39a303822 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -2034,6 +2034,19 @@ private: "}\n"; ASSERT_EQUALS(false, testValueOfX(code, 6U, 3)); TODO_ASSERT_EQUALS(true, false, testValueOfX(code, 6U, 2)); + + code = "struct Fred {\n" + " static void Create(std::unique_ptr wilma);\n" + " Fred(std::unique_ptr wilma);\n" + " std::unique_ptr mWilma;\n" + "};\n" + "void Fred::Create(std::unique_ptr wilma) {\n" + " auto fred = std::make_shared(std::move(wilma));\n" + " fred->mWilma.reset();\n" + "}\n" + "Fred::Fred(std::unique_ptr wilma)\n" + " : mWilma(std::move(wilma)) {}\n"; + ASSERT_EQUALS(0, tokenValues(code, "mWilma (").size()); } void valueFlowAfterCondition() {