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 Token* parent = nullptr;
const ValueType* vt = tok->valueType(); const ValueType* vt = tok->valueType();
std::vector<ValueType> vtParents = getParentValueTypes(tok, settings, &parent); std::vector<ValueType> vtParents = getParentValueTypes(tok, settings, &parent);
if (vt) { for (const ValueType& vtParent : vtParents) {
for (const ValueType& vtParent : vtParents) { if (isLifetimeBorrowed(vt, &vtParent))
if (isLifetimeBorrowed(vt, &vtParent)) return true;
return true; if (isLifetimeOwned(vt, &vtParent))
if (isLifetimeOwned(vt, &vtParent)) return false;
return false;
}
} }
if (parent) { if (parent) {
if (isDifferentType(tok, parent)) if (isDifferentType(tok, parent))

View File

@ -2573,6 +2573,12 @@ private:
" h = 1;\n" " h = 1;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); 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() void danglingLifetimeContainerView()