Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ static const ValueFlow::Value* getInnerLifetime(const Token* tok,
if (!tok)
return nullptr;
for (const ValueFlow::Value& val : tok->values()) {
if (!val.isLocalLifetimeValue())
if (!val.isLocalLifetimeValue() && !val.isArgumentLifetimeValue())
continue;
if (contains({ValueFlow::Value::LifetimeKind::Address,
ValueFlow::Value::LifetimeKind::SubObject,
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ static std::vector<ValueFlow::LifetimeToken> getLifetimeTokens(const Token* tok,
if (Token::Match(tok->tokAt(-2), ". %name% (") && tok->tokAt(-2)->originalName() != "->" && astIsContainer(tok->tokAt(-2)->astOperand1())) {
const Library::Container* library = getLibraryContainer(tok->tokAt(-2)->astOperand1());
const Library::Container::Yield y = library->getYield(tok->strAt(-1));
if (y == Library::Container::Yield::AT_INDEX || y == Library::Container::Yield::ITEM) {
if (contains({Library::Container::Yield::AT_INDEX, Library::Container::Yield::ITEM, Library::Container::Yield::BUFFER, Library::Container::Yield::BUFFER_NT}, y)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not correct, The buffer returns a pointer not a reference so it should not be used here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The function is called getLifetimeTokens() though?

Copy link
Contributor

Choose a reason for hiding this comment

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

Its get the lifetime of a reference. Thats why the it only follows the function that return references(see here). Its similar to followReferences, but different as it will get the parent lifetime of a reference as well.

Originally, it was called getLifetimeVariable(and there is still an overload for this), but was renamed to getLifetimeTokens so it can handle expressions and multiple lifetimes.

The lifetime of pointers are set in valueflow in the token. For pointer to containers data() method, we set the lifetime here:

master.errorPath.emplace_back(parent->tokAt(2), "Pointer to container is created here.");

errorPath.emplace_back(tok->previous(), "Accessing container.");
return ValueFlow::LifetimeToken::setAddressOf(
getLifetimeTokens(tok->tokAt(-2)->astOperand1(), escape, std::move(errorPath), pred, settings, depth - 1),
Expand Down
3 changes: 3 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3960,6 +3960,9 @@ class TestOther : public TestFixture {

check("void push(V& v) { v.push_back({ .x = 1 }); }"); // #14010
ASSERT_EQUALS("", errout_str());

check("size_t* f(std::array<uint8_t, 128>& a) { return reinterpret_cast<size_t*>(a.data()); }\n"); // #14074
ASSERT_EQUALS("", errout_str());
}

void constParameterCallback() {
Expand Down