[libcxx-commits] [libcxx] [libc++] Simplify std::function implementation further (PR #145153)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Jun 30 11:44:30 PDT 2025
================
@@ -206,12 +204,13 @@ class __value_func<_Rp(_ArgTypes...)> {
_LIBCPP_HIDE_FROM_ABI explicit __value_func(_Fp&& __f) : __f_(nullptr) {
typedef __function::__func<_Fp, _Rp(_ArgTypes...)> _Fun;
- if (__function::__not_null(__f)) {
- if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value) {
- __f_ = ::new (std::addressof(__buf_)) _Fun(std::move(__f));
- } else {
- __f_ = new _Fun(std::move(__f));
- }
+ if (__function::__is_null(__f))
+ return;
+
+ if (sizeof(_Fun) <= sizeof(__buf_) && is_nothrow_copy_constructible<_Fp>::value) {
+ __f_ = ::new (std::addressof(__buf_)) _Fun(std::move(__f));
----------------
EricWF wrote:
I think this needs to be `::new ((void*)std::addressof(__buf_)) _Fun(std::move(__f));` otherwise users can hijack the call to placement new using an overload like
```
template <class T>
void* operator new(size_t, T*) = delete;
```
See https://godbolt.org/z/Wfhes8Mff
https://github.com/llvm/llvm-project/pull/145153
More information about the libcxx-commits
mailing list