Skip to content

Commit e998cd1

Browse files
authored
Partial fix for 10393: FP returnDanglingLifetime when constructing string from iterators [inconclusive] (cppcheck-opensource#3536)
1 parent e20ddd5 commit e998cd1

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6615,8 +6615,13 @@ void SymbolDatabase::setValueTypeInTokenList(bool reportDebugWarnings, Token *to
66156615
const Scope *functionScope = tok->scope();
66166616
while (functionScope && functionScope->isExecutable() && functionScope->type != Scope::eLambda && functionScope->type != Scope::eFunction)
66176617
functionScope = functionScope->nestedIn;
6618-
if (functionScope && functionScope->type == Scope::eFunction && functionScope->function && functionScope->function->retDef)
6619-
setValueType(tok, ValueType::parseDecl(functionScope->function->retDef, mSettings));
6618+
if (functionScope && functionScope->type == Scope::eFunction && functionScope->function &&
6619+
functionScope->function->retDef) {
6620+
ValueType vt = ValueType::parseDecl(functionScope->function->retDef, mSettings);
6621+
setValueType(tok, vt);
6622+
if (Token::simpleMatch(tok, "return {"))
6623+
setValueType(tok->next(), vt);
6624+
}
66206625
} else if (tok->variable()) {
66216626
setValueType(tok, *tok->variable());
66226627
if (!tok->variable()->valueType() && tok->valueType())

test/testautovariables.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,12 @@ class TestAutoVariables : public TestFixture {
24562456
" return &*seq.begin();\n"
24572457
"}\n");
24582458
ASSERT_EQUALS("", errout.str());
2459+
2460+
check("std::string f(std::string Str, int first, int last) {\n"
2461+
" return { Str.begin() + first, Str.begin() + last + 1 };\n"
2462+
"}\n",
2463+
true);
2464+
ASSERT_EQUALS("", errout.str());
24592465
}
24602466

24612467
void danglingLifetimeContainerView()

0 commit comments

Comments
 (0)