Skip to content

Commit a1078f4

Browse files
authored
Fix 11980: False positive returnDanglingLifetime returning pointer to stack array as std::string (by way of struct return value constructor) (#5445)
1 parent 844ed2b commit a1078f4

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/astutils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,8 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
724724
const Scope* scope = t->classScope;
725725
// Check for aggregate constructors
726726
if (scope && scope->numConstructors == 0 && t->derivedFrom.empty() &&
727-
(t->isClassType() || t->isStructType()) && numberOfArguments(ftok) < scope->varlist.size()) {
727+
(t->isClassType() || t->isStructType()) && numberOfArguments(ftok) <= scope->varlist.size() &&
728+
!scope->varlist.empty()) {
728729
assert(argn < scope->varlist.size());
729730
auto it = std::next(scope->varlist.cbegin(), argn);
730731
if (it->valueType())

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4602,7 +4602,7 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
46024602
const Variable& var = *it;
46034603
if (var.isReference() || var.isRValueReference()) {
46044604
ls.byRef(tok, tokenlist, errorLogger, settings);
4605-
} else {
4605+
} else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) {
46064606
ls.byVal(tok, tokenlist, errorLogger, settings);
46074607
}
46084608
it++;

test/testautovariables.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3750,6 +3750,15 @@ class TestAutoVariables : public TestFixture {
37503750
" return A{y, x};\n"
37513751
"}");
37523752
ASSERT_EQUALS("", errout.str());
3753+
3754+
check("struct a {\n"
3755+
" std::string m;\n"
3756+
"};\n"
3757+
"a f() {\n"
3758+
" std::array<char, 1024> m {};\n"
3759+
" return { m.data() };\n"
3760+
"}\n");
3761+
ASSERT_EQUALS("", errout.str());
37533762
}
37543763

37553764
void danglingLifetimeInitList() {

0 commit comments

Comments
 (0)