Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12568 FP invalidLifetime with std::unique_ptr #6236

Merged
merged 6 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ bool astIsIterator(const Token *tok)
return tok && tok->valueType() && tok->valueType()->type == ValueType::Type::ITERATOR;
}

bool astIsContainer(const Token* tok) {
return getLibraryContainer(tok) != nullptr && !astIsIterator(tok);
const Library::Container* astIsContainer(const Token* tok) {
const Library::Container* container = getLibraryContainer(tok);
return container != nullptr && !astIsIterator(tok) ? container : nullptr;
}

bool astIsContainerView(const Token* tok)
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool astIsUniqueSmartPointer(const Token* tok);

bool astIsIterator(const Token *tok);

bool astIsContainer(const Token *tok);
const Library::Container* astIsContainer(const Token *tok);

bool astIsContainerView(const Token* tok);
bool astIsContainerOwned(const Token* tok);
Expand Down
3 changes: 2 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4455,6 +4455,7 @@ static void valueFlowLifetimeFunction(Token *tok, TokenList &tokenlist, ErrorLog
if (Token::Match(tok->astParent(), ". %name% (") && astIsRHS(tok))
memtok = tok->astParent()->astOperand1();
const int returnContainer = settings.library.returnValueContainer(tok);
const Library::Container* container{};
if (returnContainer >= 0) {
std::vector<const Token *> args = getArguments(tok);
for (int argnr = 1; argnr <= args.size(); ++argnr) {
Expand Down Expand Up @@ -4489,7 +4490,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)) {
(container = astIsContainer(memtok)) && !container->stdStringLike) {
chrchr-github marked this conversation as resolved.
Show resolved Hide resolved
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
Loading