Skip to content

Commit

Permalink
Fix #11802 FP stlcstr for string in shared_ptr copied elsewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jul 10, 2023
1 parent cc38ef4 commit b5eb769
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2054,7 +2054,7 @@ void CheckStl::string_c_str()
if (Token::Match(refTok, "%var% = %var% .|;|["))
refToNonLocal = !isLocal(refTok->tokAt(2));
}
ptrOrRef = refToNonLocal || (tok2->variable() && tok2->variable()->isPointer());
ptrOrRef = refToNonLocal || (tok2->variable() && (tok2->variable()->isPointer() || tok2->variable()->isSmartPointer()));
}
while (tok2) {
if (Token::Match(tok2, "%var% .|::")) {
Expand Down
9 changes: 9 additions & 0 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4261,6 +4261,15 @@ class TestStl : public TestFixture {
" std::string_view sv(s.data(), 13);\n"
"}\n");
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() {
Expand Down

0 comments on commit b5eb769

Please sign in to comment.