diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d97cd4c62a7..69afe396a12 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2647,6 +2647,8 @@ static void valueFlowLifetimeClassConstructor(Token* tok, if (it == scope->varlist.cend()) return; const Variable& var = *it; + if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view) + return; // TODO: check in isLifetimeBorrowed()? if (var.isReference() || var.isRValueReference()) { ls.byRef(tok, tokenlist, errorLogger, settings); } else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index fe43f582325..31e5bdfabad 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -148,7 +148,7 @@ class TestAutoVariables : public TestFixture { TEST_CASE(danglingLifetime); TEST_CASE(danglingLifetimeFunction); TEST_CASE(danglingLifetimeUserConstructor); - TEST_CASE(danglingLifetimeAggegrateConstructor); + TEST_CASE(danglingLifetimeAggregateConstructor); TEST_CASE(danglingLifetimeInitList); TEST_CASE(danglingLifetimeImplicitConversion); TEST_CASE(danglingTemporaryLifetime); @@ -3796,7 +3796,7 @@ class TestAutoVariables : public TestFixture { ASSERT_EQUALS("", errout_str()); } - void danglingLifetimeAggegrateConstructor() { + void danglingLifetimeAggregateConstructor() { check("struct A {\n" " const int& x;\n" " int y;\n" @@ -3893,6 +3893,30 @@ class TestAutoVariables : public TestFixture { " return { m.data() };\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("struct S { std::string s; };\n" // #13167 + "std::vector f() {\n" + " std::vector v;\n" + " {\n" + " std::string a{ \"abc\" };\n" + " v.push_back({ a.c_str() });\n" + " }\n" + " return v;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("struct S { std::string_view sv; };\n" + "std::vector f() {\n" + " std::vector v;\n" + " {\n" + " std::string a{ \"abc\" };\n" + " v.push_back({ a.c_str() });\n" + " }\n" + " return v;\n" + "}\n"); + ASSERT_EQUALS( + "[test.cpp:6] -> [test.cpp:6] -> [test.cpp:6] -> [test.cpp:5] -> [test.cpp:8]: (error) Returning object that points to local variable 'a' that will be invalid when returning.\n", + errout_str()); } void danglingLifetimeInitList() {