<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/125257>125257</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] C++ constructor arguments should support [[clang::lifetimebound]]
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ddkilzer
</td>
</tr>
</table>
<pre>
The `[[clang::lifetimebound]]` attribute should be supported on C++ constructor arguments when the class takes a reference to an object whose lifetime must extend past an instance of the class.
In other words, the `this` object returned from the constructor should have its lifetime bound to one or more of its constructor arguments.
This should also apply to `struct` constructors in C++. (Changing `class buffer_view` to `struct buffer_view` below demonstrates this in Godbolt.)
https://godbolt.org/z/x665eWPqd
```
#include <iostream>
#include <span>
#include <string>
class buffer_view {
public:
buffer_view(const std::basic_string<char>& s [[clang::lifetimebound]]);
std::basic_string<char> s();
private:
std::span<const char> m_view;
};
buffer_view::buffer_view(const std::basic_string<char>& s)
: m_view({ s.data(), s.size() })
{
}
std::basic_string<char> buffer_view::s()
{
return { m_view.data(), m_view.size() };
}
int main(int argc, char** argv)
{
buffer_view bv { std::string("foo") }; // expected warning
std::cout << bv.s(); // use-after-free crash with address sanitizer
return 0;
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyclc2OozgQx5_GuZQ6IkX4yIEDSU9We9vDSHscGVwE74DNukzS00-_MpBO0j1St1ZCCoH6-P2LKpdk1idDVIhkL5LnlRx9a12h1E_dvZJbVVb9Kr63BCKNJpN93UlzEnEp4rLTDXndU2VHo0TyHK40Aum909XoCbi1Y6egIuBxGKzzpMAaOAjcC9xDbQ17N9beOpDuNPZkPMOlJQO-Jag7yQxe_iQGCY4acmRqAm9BGrDVP1R7uLSWCa4k0I_sgV48GQWDZB8stWEvg6NtbnHXIipFVP5pwPqWHFysUyzwMFmINPKt5iBmSePIj86QgsbZfo5yB7_obOWZQ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