Skip to content

Commit 3edb10a

Browse files
authored
Fix 11024: FP returnDanglingLifetime with c_str() passed to constructor (#4072)
1 parent 9f7c342 commit 3edb10a

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,8 @@ static void valueFlowLifetimeConstructor(Token* tok,
38883888
const Token* expr = tok2->astOperand2();
38893889
if (!var)
38903890
continue;
3891+
if (!isLifetimeBorrowed(expr, settings))
3892+
continue;
38913893
const Variable* argvar = getLifetimeVariable(expr);
38923894
if (var->isReference() || var->isRValueReference()) {
38933895
if (argvar && argvar->isArgument() && (argvar->isReference() || argvar->isRValueReference())) {

test/testautovariables.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,17 @@ class TestAutoVariables : public TestFixture {
32693269
"}\n",
32703270
true);
32713271
ASSERT_EQUALS("", errout.str());
3272+
3273+
check("struct S {\n"
3274+
" std::string msg;\n"
3275+
" explicit S(const char* m) : msg(m) {}\n"
3276+
"};\n"
3277+
"S f() {\n"
3278+
" std::string s(\"abc\");\n"
3279+
" return S(s.c_str());\n"
3280+
"}\n",
3281+
true);
3282+
ASSERT_EQUALS("", errout.str());
32723283
}
32733284

32743285
void danglingLifetimeAggegrateConstructor() {

0 commit comments

Comments
 (0)