[LifetimeSafety] `-Wlifetime-safety-invalidation`: false positive when using a `unique_ptr` after `reset()`
$ cat test.cpp
#include <memory>
void true_positive(std::unique_ptr<int> &p) {
int *raw = p.get(); // expected: "parameter 'p' is later invalidated"
p.reset();
(void)*raw; // use of dangling pointer
}
bool false_positive(std::unique_ptr<int> &p, int *new_val) {
p.reset(new_val);
return static_cast<bool>(p); // spurious: "parameter 'p' is later invalidated"
}
$ clang++ -fsyntax-only -std=c++17 -Wlifetime-safety-invalidation test.cpp
test.cpp:3:20: warning: parameter 'p' is later invalidated [-Wlifetime-safety-invalidation]
3 | void true_positive(std::unique_ptr<int> &p) {
| ^~~~~~~~~~~~~~~~~~~~~~~
test.cpp:5:5: note: parameter 'p' is invalidated here
5 | p.reset();
| ~~^~~~~~~
test.cpp:6:10: note: later used here
6 | (void)*raw; // use of dangling pointer
| ^~~
test.cpp:9:21: warning: parameter 'p' is later invalidated [-Wlifetime-safety-invalidation]
9 | bool false_positive(std::unique_ptr<int> &p, int *new_val) {
| ^~~~~~~~~~~~~~~~~~~~~~~
test.cpp:10:5: note: parameter 'p' is invalidated here
10 | p.reset(new_val);
| ~~^~~~~~~~~~~~~~
test.cpp:11:28: note: later used here
11 | return static_cast<bool>(p); // spurious: "parameter 'p' is later invalidated"
| ^
2 warnings generated.
This looks like a false positive. unique_ptr::reset() invalidates raw pointers previously obtained via get(), not the unique_ptr object itself.
Introduced by #194907.
@NeKon69
@llvm/issue-subscribers-clang-temporal-safety
Author: Akira Hatanaka (ahatanak)
void true_positive(std::unique_ptr<int> &p) { int *raw = p.get(); // expected: "parameter 'p' is later invalidated" p.reset(); (void)*raw; // use of dangling pointer }
bool false_positive(std::unique_ptr<int> &p, int *new_val) { p.reset(new_val); return static_cast<bool>(p); // spurious: "parameter 'p' is later invalidated" }
$ clang++ -fsyntax-only -std=c++17 -Wlifetime-safety-invalidation test.cpp
test.cpp:3:20: warning: parameter 'p' is later invalidated [-Wlifetime-safety-invalidation] 3 | void true_positive(std::unique_ptr<int> &p) { | ^~~~~~~~~~~~~~~~~~~~~~~ test.cpp:5:5: note: parameter 'p' is invalidated here 5 | p.reset(); | ~~^~~~~~~ test.cpp:6:10: note: later used here 6 | (void)*raw; // use of dangling pointer | ^~~ test.cpp:9:21: warning: parameter 'p' is later invalidated [-Wlifetime-safety-invalidation] 9 | bool false_positive(std::unique_ptr<int> &p, int *new_val) { | ^~~~~~~~~~~~~~~~~~~~~~~ test.cpp:10:5: note: parameter 'p' is invalidated here 10 | p.reset(new_val); | ~~^~~~~~~~~~~~~~ test.cpp:11:28: note: later used here 11 | return static_cast<bool>(p); // spurious: "parameter 'p' is later invalidated" | ^ 2 warnings generated.
This looks like a false positive. `unique_ptr::reset()` invalidates raw pointers previously obtained via `get()`, not the `unique_ptr` object itself.
Introduced by #<!-- -->194907.
</details>
I think this issue is more of a general problem with invalidations currently, not something specific to std::unique_ptr::reset, see - https://godbolt.org/z/qT18xGWbW (invalidations are currently highly experimental, so it's expected that they produce a lot of false positives)
But still nice to keep track of IMO.
Another example
#include <string>
auto f(auto & s) -> void {
s.resize(0);
auto x = s.begin();
}
auto g() -> void {
auto s = std::string();
f(s);
}
https://godbolt.org/z/YWeTGq919