Fix 11980: False positive returnDanglingLifetime returning pointer to stack array as std::string (by way of struct return value constructor) (#5445)

This commit is contained in:
Paul Fultz II 2023-09-13 14:26:37 -05:00 committed by GitHub
parent 844ed2bf22
commit a1078f446a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 2 deletions

View File

@ -724,7 +724,8 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
const Scope* scope = t->classScope;
// Check for aggregate constructors
if (scope && scope->numConstructors == 0 && t->derivedFrom.empty() &&
(t->isClassType() || t->isStructType()) && numberOfArguments(ftok) < scope->varlist.size()) {
(t->isClassType() || t->isStructType()) && numberOfArguments(ftok) <= scope->varlist.size() &&
!scope->varlist.empty()) {
assert(argn < scope->varlist.size());
auto it = std::next(scope->varlist.cbegin(), argn);
if (it->valueType())

View File

@ -4602,7 +4602,7 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
const Variable& var = *it;
if (var.isReference() || var.isRValueReference()) {
ls.byRef(tok, tokenlist, errorLogger, settings);
} else {
} else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) {
ls.byVal(tok, tokenlist, errorLogger, settings);
}
it++;

View File

@ -3750,6 +3750,15 @@ private:
" return A{y, x};\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct a {\n"
" std::string m;\n"
"};\n"
"a f() {\n"
" std::array<char, 1024> m {};\n"
" return { m.data() };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeInitList() {