[LifetimeSafety] Detect use-after-scope through a field and member calls
#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_.
@llvm/issue-subscribers-clang-temporal-safety
Author: Utkarsh Saxena (usx95)
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>
I will try to work on this.
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?
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.