Skip to content

Commit c221f52

Browse files
Fix #14209 FN invalidLifetime (struct returned from subfunction, regression) (danmar#8144)
1 parent ddce8d5 commit c221f52

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/valueflow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2700,7 +2700,8 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
27002700
if (isDesignatedInitializerArg)
27012701
ls.argtok = ls.argtok->astOperand2();
27022702
const Variable &var = *it;
2703-
if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view)
2703+
if (ls.argtok->valueType() && ls.argtok->valueType()->pointer &&
2704+
var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view)
27042705
return; // TODO: check in isLifetimeBorrowed()?
27052706
if (var.isReference() || var.isRValueReference()) {
27062707
ls.byRef(tok, tokenlist, errorLogger, settings);

test/testautovariables.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,6 +3957,15 @@ class TestAutoVariables : public TestFixture {
39573957
"[test.cpp:6:30] -> [test.cpp:6:30] -> [test.cpp:6:21] -> [test.cpp:5:21] -> [test.cpp:8:12]: (error) Returning object that points to local variable 'a' that will be invalid when returning. [returnDanglingLifetime]\n",
39583958
errout_str());
39593959

3960+
check("struct S { const std::string& r; };\n" // #14209
3961+
"S f() {\n"
3962+
" std::string s;\n"
3963+
" return S{ s };\n"
3964+
"}\n");
3965+
ASSERT_EQUALS(
3966+
"[test.cpp:4:15] -> [test.cpp:3:17] -> [test.cpp:4:13]: (error) Returning object that points to local variable 's' that will be invalid when returning. [returnDanglingLifetime]\n",
3967+
errout_str());
3968+
39603969
check("struct A { int& x; };\n" // #14247
39613970
"A f() {\n"
39623971
" int x = 0;\n"

0 commit comments

Comments
 (0)