llvm / llvm/llvm-project

[LifetimeSafety] False positive after adding to an std::vector

Open
#222,537 2 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

The following code produces a false positive lifetime warning about `vec` being invalidated after `push_back()`. Same goes for `emplace_back()`.
```cpp
void repro(std::vector& vec)
{
vec.push_back(1);
vec[0] = 2;
}
```

Compiled with `-std=c++20 -Wlifetime-safety-invalidation`

```
:3:12: warning: parameter 'vec' is later invalidated [-Wlifetime-safety-invalidation]
3 | void repro(std::vector& vec)
| ^~~~~~~~~~~~~~~~~~~~~
:5:9: note: parameter 'vec' is invalidated here
5 | vec.push_back(1);
| ~~~~^~~~~~~~~~~~
:6:5: note: later used here
6 | vec[0] = 2;
| ^~~
```

Here is the code on godbolt: https://godbolt.org/z/xvrfo7rj6
This issue appeared with 23.1.0 and still exists in 23.1.1.

```
clang version 23.1.1 (https://github.com/llvm/llvm-project 6dfe1677ab8dffbc6ec13d53a1e0215d75147689)
Target: x86_64-pc-windows-msvc
Thread model: posix
Build config: +alloc:rpmalloc
```

Contributor guide

Open the contributing guide

Research direction

Start with the provided std::vector repro and run Clang with -std=c++20 -Wlifetime-safety-invalidation, comparing push_back() and emplace_back(). Trace the lifetime-safety invalidation handling for vector mutation and add or update coverage for this case; done means the false warning is gone while genuine invalidation warnings remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.