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:
parent
844ed2bf22
commit
a1078f446a
|
@ -724,7 +724,8 @@ std::vector<ValueType> getParentValueTypes(const Token* tok, const Settings* set
|
||||||
const Scope* scope = t->classScope;
|
const Scope* scope = t->classScope;
|
||||||
// Check for aggregate constructors
|
// Check for aggregate constructors
|
||||||
if (scope && scope->numConstructors == 0 && t->derivedFrom.empty() &&
|
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());
|
assert(argn < scope->varlist.size());
|
||||||
auto it = std::next(scope->varlist.cbegin(), argn);
|
auto it = std::next(scope->varlist.cbegin(), argn);
|
||||||
if (it->valueType())
|
if (it->valueType())
|
||||||
|
|
|
@ -4602,7 +4602,7 @@ static void valueFlowLifetimeClassConstructor(Token* tok,
|
||||||
const Variable& var = *it;
|
const Variable& var = *it;
|
||||||
if (var.isReference() || var.isRValueReference()) {
|
if (var.isReference() || var.isRValueReference()) {
|
||||||
ls.byRef(tok, tokenlist, errorLogger, settings);
|
ls.byRef(tok, tokenlist, errorLogger, settings);
|
||||||
} else {
|
} else if (ValueFlow::isLifetimeBorrowed(ls.argtok, settings)) {
|
||||||
ls.byVal(tok, tokenlist, errorLogger, settings);
|
ls.byVal(tok, tokenlist, errorLogger, settings);
|
||||||
}
|
}
|
||||||
it++;
|
it++;
|
||||||
|
|
|
@ -3750,6 +3750,15 @@ private:
|
||||||
" return A{y, x};\n"
|
" return A{y, x};\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void danglingLifetimeInitList() {
|
||||||
|
|
Loading…
Reference in New Issue