Fix 10954: False positive: returnDanglingLifetime when using c_str() (#4006)

This commit is contained in:
Paul Fultz II 2022-04-12 01:09:13 -05:00 committed by GitHub
parent 61b87bcc60
commit 6c82fb05a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

View File

@ -3441,13 +3441,11 @@ bool isLifetimeBorrowed(const Token *tok, const Settings *settings)
const Token* parent = nullptr;
const ValueType* vt = tok->valueType();
std::vector<ValueType> vtParents = getParentValueTypes(tok, settings, &parent);
if (vt) {
for (const ValueType& vtParent : vtParents) {
if (isLifetimeBorrowed(vt, &vtParent))
return true;
if (isLifetimeOwned(vt, &vtParent))
return false;
}
for (const ValueType& vtParent : vtParents) {
if (isLifetimeBorrowed(vt, &vtParent))
return true;
if (isLifetimeOwned(vt, &vtParent))
return false;
}
if (parent) {
if (isDifferentType(tok, parent))

View File

@ -2573,6 +2573,12 @@ private:
" h = 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("std::string f(std::string s) {\n"
" std::string ss = (\":\" + s).c_str();\n"
" return ss;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeContainerView()