llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LifetimeSafety] Detect use-after-scope through a field and member calls

Open usx95 opened this issue 9 months ago • 4 comments

#include <iostream>
#include <string>
#include <string_view>

std::string GLOBAL = "global string";

struct S {
    std::string* p_;
    void bar();
    void foo3() {
        {
            std::string stack;
            p_ = &stack;  // warning: participates in use-after-scope
        }  // destroyed here.
        bar();        // later used here (via use of 'this').
        p_ = &GLOBAL;  // this does not save escape now.
    }
};

This is not detected. https://godbolt.org/z/6KebzPo8K The call to bar uses this which should count as a use of p_.

usx95 avatar Feb 23 '26 21:02 usx95

@llvm/issue-subscribers-clang-temporal-safety

Author: Utkarsh Saxena (usx95)

```cpp #include <iostream> #include <string> #include <string_view>

std::string GLOBAL = "global string";

struct S { std::string* p_; void bar(); void foo3() { { std::string stack; p_ = &stack; // warning: participates in use-after-scope } // destroyed here. bar(); // later used here (via use of 'this'). p_ = &GLOBAL; // this does not save escape now. } };

This is not detected. https://godbolt.org/z/6KebzPo8K
The call to bar uses `this` which should count as a use of `p_`.
</details>

llvmbot avatar Feb 23 '26 21:02 llvmbot

I will try to work on this.

NeKon69 avatar Apr 10 '26 19:04 NeKon69

It looks like this all comes back to #189240, because OriginList for this does not currently store origins for inner fields. If that’s not the right way to think about it, could you give a hint about the intended model?

NeKon69 avatar Apr 12 '26 11:04 NeKon69

My idea is that when we deal with member function calls, we issue a use-fact for each of the field origins. This should be enough to issue a UAS warning needed above.

usx95 avatar Apr 12 '26 13:04 usx95