Skip to content

Commit

Permalink
Fix #12568 FP invalidLifetime with std::unique_ptr (#6236)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Apr 6, 2024
1 parent 15a818c commit 7a546a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ bool astIsContainer(const Token* tok) {
return getLibraryContainer(tok) != nullptr && !astIsIterator(tok);
}

bool astIsNonStringContainer(const Token* tok)
{
const Library::Container* container = getLibraryContainer(tok);
return container && !container->stdStringLike && !astIsIterator(tok);
}

bool astIsContainerView(const Token* tok)
{
const Library::Container* container = getLibraryContainer(tok);
Expand Down
1 change: 1 addition & 0 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ bool astIsUniqueSmartPointer(const Token* tok);
bool astIsIterator(const Token *tok);

bool astIsContainer(const Token *tok);
bool astIsNonStringContainer(const Token *tok);

bool astIsContainerView(const Token* tok);
bool astIsContainerOwned(const Token* tok);
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4489,7 +4489,7 @@ static void valueFlowLifetimeFunction(Token *tok, TokenList &tokenlist, ErrorLog
tok->next(), tokenlist, errorLogger, settings);
}
} else if (memtok && Token::Match(tok->astParent(), ". push_back|push_front|insert|push|assign") &&
astIsContainer(memtok)) {
astIsNonStringContainer(memtok)) {
std::vector<const Token *> args = getArguments(tok);
const std::size_t n = args.size();
if (n > 1 && Token::typeStr(args[n - 2]) == Token::typeStr(args[n - 1]) &&
Expand Down
10 changes: 10 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,16 @@ class TestAutoVariables : public TestFixture {
" return ss;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f() {\n" // #12568
" std::string str;\n"
" {\n"
" std::unique_ptr<char[]> b(new char[1]{});\n"
" str.assign(b.get());\n"
" }\n"
" std::cerr << str;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void danglingLifetimeContainerView()
Expand Down

0 comments on commit 7a546a8

Please sign in to comment.