Skip to content

Commit 0911ee6

Browse files
Fix #14818 FP returnDanglingLifetime when passing c_str() to function (#8750)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 8effc6c commit 0911ee6

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,8 @@ static void valueFlowLifetimeFunction(Token *tok, const TokenList &tokenlist, Er
25422542
for (const Token* returnTok : returns) {
25432543
if (returnTok == tok)
25442544
continue;
2545+
if (!ValueFlow::isLifetimeBorrowed(returnTok, settings))
2546+
return;
25452547
const Variable *returnVar = ValueFlow::getLifetimeVariable(returnTok, settings);
25462548
if (returnVar && returnVar->isArgument() && (returnVar->isConst() || !isVariableChanged(returnVar, settings))) {
25472549
LifetimeStore ls = LifetimeStore::fromFunctionArg(f, tok, returnVar, tokenlist, settings, errorLogger);

test/testautovariables.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,6 +2962,15 @@ class TestAutoVariables : public TestFixture {
29622962
" int m;\n"
29632963
"}\n");
29642964
ASSERT_EQUALS("", errout_str());
2965+
2966+
check("std::string to_string(const char* p) {\n" // #14818
2967+
" return p;\n"
2968+
"}\n"
2969+
"std::string get() {\n"
2970+
" std::string s;\n"
2971+
" return to_string(s.c_str());\n"
2972+
"}\n");
2973+
ASSERT_EQUALS("", errout_str());
29652974
}
29662975

29672976
void danglingLifetimeContainerView()

0 commit comments

Comments
 (0)