llvm / llvm/llvm-project

[LifetimeSafety] Stricter warnings for params escaping to globals

Open
#188,293 8 comments 0 reactions 0 assignees View on GitHub
clang:temporal-safety
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.