[LifetimeSafety] Stricter warnings for params escaping to globals
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider the following function:
```c++
#include
struct Buffer {
std::span contents() [[clang::lifetimebound]] { }
};
std::span global;
void test() {
Buffer buffer;
auto span = buffer.contents();
global = span; // WARN
}
```
We already warn for the code above as stack-owned memory is being escaped.
On the other hand, we do not warn for the following code:
```c++
#include
struct Buffer {
std::span contents() [[clang::lifetimebound]] { }
};
std::span global;
void test(Buffer *buffer) {
auto span = buffer->contents();
global = span; // no warning
}
```
While strictly speaking we cannot be sure there is a lifetime issue in the second snippet, but this function is very error prone and passing in any buffer with a non-static lifetime is potentially problematic. In strict mode, maybe we should warn for this snippet as well?
Contributor guide
Assessment
This issue has not been assessed yet.