Skip to content

[Clang] -Wdangling does not emit warning when a dangerous call invoked in STL #140483

@denzor200

Description

@denzor200

Please be patient reading all examples below - root of the evil hidden in the fact that std::pair<const A, B> not the same as std::pair<A, B> but the first one might be implicitly converted to the second one. Such implicit conversion might lead to hidden UB if not carefully coded.

Assume we have a proj lambda like this:

const auto proj = 
        [](const std::pair<std::string, int>& node [[clang::lifetimebound]])
         -> const std::pair<std::string, int>& {
            return node;
        };

Let's call this lambda directly by creating the problem with dangling reference artifically:

std::pair<const std::string, int> aa;
const auto& a = proj(aa);

Then we will watch warning: temporary bound to local reference 'a' will be destroyed at the end of the full-expression [-Wdangling] emited - that's fine.

Now let's create conditions under which the function will be called outside of user code, but the problem with the dangling reference will still be relevant:

std::map<std::string, int> counters = { {"hello", 5}, {"world", 5} };
for (const auto& node : counters | std::views::transform(proj)) {
    (void)node; // dangling
}

And here we will not see that warning emited - that's the problem that I am creating this issue about and which should be fixed.

The full snippet here: https://godbolt.org/z/1Yaj1s6Wf

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:temporal-safetyIssue/FR relating to the lifetime analysis in Clang (-Wdangling, -Wreturn-local-addr)

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions