From 1322c819e3eddb85949a3a5018483600a8b8c0da Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 23 Jul 2026 15:28:01 +0200 Subject: [PATCH 1/3] Update valueflow.cpp --- lib/valueflow.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 096e3d56f56..07c191fa8d3 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2536,6 +2536,8 @@ static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, Er } if (Function::returnsReference(f)) return; + if (astIsContainerString(tok->next()) && !astIsContainerView(tok->next())) + return; std::vector returns = Function::findReturns(f); const bool inconclusive = returns.size() > 1; bool update = false; From 0a7f69379cd1b281b124a5f89afd620702b393d4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 23 Jul 2026 15:28:38 +0200 Subject: [PATCH 2/3] Update testautovariables.cpp --- test/testautovariables.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index df421d6879c..7774d7fa733 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2962,6 +2962,15 @@ class TestAutoVariables : public TestFixture { " int m;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("std::string to_string(const char* p) {\n" // #14818 + " return p;\n" + "}\n" + "std::string get() {\n" + " std::string s;\n" + " return to_string(s.c_str());\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void danglingLifetimeContainerView() From 9e270dc78f82f9b4ff22fc69307b2fe8ed8da95b Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Thu, 23 Jul 2026 18:57:25 +0200 Subject: [PATCH 3/3] Use isLifetimeBorrowed() --- lib/valueflow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 07c191fa8d3..0d08cf2ba07 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2536,14 +2536,14 @@ static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, Er } if (Function::returnsReference(f)) return; - if (astIsContainerString(tok->next()) && !astIsContainerView(tok->next())) - return; std::vector returns = Function::findReturns(f); const bool inconclusive = returns.size() > 1; bool update = false; for (const Token* returnTok : returns) { if (returnTok == tok) continue; + if (!ValueFlow::isLifetimeBorrowed(returnTok, settings)) + return; const Variable *returnVar = ValueFlow::getLifetimeVariable(returnTok, settings); if (returnVar && returnVar->isArgument() && (returnVar->isConst() || !isVariableChanged(returnVar, settings))) { LifetimeStore ls = LifetimeStore::fromFunctionArg(f, tok, returnVar, tokenlist, settings, errorLogger);