Issue 125257
Summary [clang] C++ constructor arguments should support [[clang::lifetimebound]]
Labels clang
Assignees
Reporter ddkilzer
    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;
}
```

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to