Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 13 additions & 9 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4629,6 +4629,17 @@ static bool isConvertedToView(const Token* tok, const Settings* settings)
});
}

static bool isContainerOfPointers(const Token* tok, const Settings* settings)
{
if (!tok)
{
return true;
}

ValueType vt = ValueType::parseDecl(tok, settings, true); // TODO: set isCpp
return vt.pointer > 0;
}

static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, ErrorLogger *errorLogger, const Settings *settings)
{
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
Expand Down Expand Up @@ -4781,21 +4792,14 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, Erro
if (!Token::Match(parent, ". %name% ("))
continue;

bool isContainerOfPointers = true;
const Token* containerTypeToken = tok->valueType()->containerTypeToken;
if (containerTypeToken) {
ValueType vt = ValueType::parseDecl(containerTypeToken, settings, true); // TODO: set isCpp
isContainerOfPointers = vt.pointer > 0;
}

ValueFlow::Value master;
master.valueType = ValueFlow::Value::ValueType::LIFETIME;
master.lifetimeScope = ValueFlow::Value::LifetimeScope::Local;

if (astIsIterator(parent->tokAt(2))) {
master.errorPath.emplace_back(parent->tokAt(2), "Iterator to container is created here.");
master.lifetimeKind = ValueFlow::Value::LifetimeKind::Iterator;
} else if ((astIsPointer(parent->tokAt(2)) && !isContainerOfPointers) ||
} else if ((astIsPointer(parent->tokAt(2)) && !isContainerOfPointers(tok->valueType()->containerTypeToken, settings)) ||
Token::Match(parent->next(), "data|c_str")) {
master.errorPath.emplace_back(parent->tokAt(2), "Pointer to container is created here.");
master.lifetimeKind = ValueFlow::Value::LifetimeKind::Object;
Expand All @@ -4816,7 +4820,7 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase* /*db*/, Erro
}
} else if (astIsContainerView(tok)) {
for (const ValueFlow::Value& v : tok->values()) {
if (!v.isLifetimeValue())
if (!v.isLocalLifetimeValue())
continue;
if (!v.tokvalue)
continue;
Expand Down
21 changes: 21 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4182,6 +4182,27 @@ class TestAutoVariables : public TestFixture {
" f(bar);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:10]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str());

check("void f(std::string_view text);\n" // #11508
"void g() {\n"
" std::string teststr;\n"
" f(teststr);"
"}\n"
"void f(std::string_view text) {"
" g(text.data());\n"
"}\n");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases should be indented.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm my reasoning was that if value has subfunction lifetime then any pointer/iterator to said in this scope should also has same lifetime. I will adjust it, thanks.

ASSERT_EQUALS("", errout.str());

check("void f(std::span<int> data);\n" // #11508
"void g() {\n"
" std::vector<int> v;\n"
" f(v);"
"}\n"
"void f(std::span<int> data) {"
" g(data.begin());\n"
"}\n");
ASSERT_EQUALS("", errout.str());

}

void deadPointer() {
Expand Down