Fix #11802 FP stlcstr for string in shared_ptr copied elsewhere (#5230)

This commit is contained in:
chrchr-github 2023-07-10 15:27:33 +02:00 committed by GitHub
parent 2cd1f0f387
commit 8d3fd88d3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -2054,7 +2054,7 @@ void CheckStl::string_c_str()
if (Token::Match(refTok, "%var% = %var% .|;|[")) if (Token::Match(refTok, "%var% = %var% .|;|["))
refToNonLocal = !isLocal(refTok->tokAt(2)); refToNonLocal = !isLocal(refTok->tokAt(2));
} }
ptrOrRef = refToNonLocal || (tok2->variable() && tok2->variable()->isPointer()); ptrOrRef = refToNonLocal || (tok2->variable() && (tok2->variable()->isPointer() || tok2->variable()->isSmartPointer()));
} }
while (tok2) { while (tok2) {
if (Token::Match(tok2, "%var% .|::")) { if (Token::Match(tok2, "%var% .|::")) {

View File

@ -4261,6 +4261,15 @@ private:
" std::string_view sv(s.data(), 13);\n" " std::string_view sv(s.data(), 13);\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("struct S { std::string x; };\n" // #11802
"std::vector<std::shared_ptr<S>> global;\n"
"const char* f() {\n"
" auto s = std::make_shared<S>();\n"
" global.push_back(s);\n"
" return s->x.c_str();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void uselessCalls() { void uselessCalls() {