llvm / llvm/llvm-project

[LifetimeSafety] Detect assignments of std::pair of view

Open
#196,470 1 comment 0 reactions 0 assignees View on GitHub
clang:temporal-safety false-negative
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This requires inference + TU analysis

https://godbolt.org/z/ch4ETEzj8

Return stack addr is detected. Use-after-scope involving assignments is not being detected.

```cpp
#include
#include
#include

std::pair ReturnDanglingPair1() {
std::string local = "hello";
return {local, "world"}; // expected-error {{address of stack memory is returned later}}
}

std::pair ReturnDanglingPair2() {
std::string local = "hello";
return {"world", local}; // expected-error {{address of stack memory is returned later}}
}

std::pair ReturnDanglingPair3() {
std::string local1 = "hello";
std::string local2 = "world";
return {local1, local2}; // expected-error 2 {{address of stack memory is returned later}}
}

// FIXME: The compiler currently doesn't catch these UseAfterScope cases for pair.
void UseAfterScope1() {
std::pair p;
{
std::string local = "hello";
p = {local, "world"};
}
(void)p.first;
}

void UseAfterScope2() {
std::pair p;
{
std::string local = "hello";
p = {"world", local};
}
(void)p.second;
}

void UseAfterScope3() {
std::pair p;
{
std::string local = "hello";
p = {local, "world"};
}
(void)p.first;
(void)p.second;
}
```

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.