[libcxx-commits] [libcxx] [libc++] Fix UB in <expected> related to "has value" flag (#68552) (PR #68733)
Jan Kokemüller via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Oct 10 13:55:09 PDT 2023
jiixyj wrote:
> Thanks for working on this! I think it would be better to move the initialization into the union, since that would make sure we're not doing anything crazy (since this would enforce the initializtation order). Right now, this looks quite brittle to me. Would you be willing to try that out?
Sure, I can try! Are you thinking of something like this?
```c++
_LIBCPP_HIDE_FROM_ABI constexpr expected(const expected& __other) noexcept(
is_nothrow_copy_constructible_v<_Tp> && is_nothrow_copy_constructible_v<_Err>) // strengthened
requires(is_copy_constructible_v<_Tp> && is_copy_constructible_v<_Err> &&
!(is_trivially_copy_constructible_v<_Tp> && is_trivially_copy_constructible_v<_Err>))
: __union_([&] {
return __other.__has_val_ ? __union_t<_Tp, _Err>(__construct_val_tag{}, __other.__union_.__val_)
: __union_t<_Tp, _Err>(__construct_unex_tag{}, __other.__union_.__unex_);
}()),
__has_val_(__other.__has_val_) {}
```
...i.e. using the `expected`'s constructor initializer list to enforce the order? Of course, that wouldn't help for the assignment and swap operators, but those tend to be tricky anyway...
https://github.com/llvm/llvm-project/pull/68733
More information about the libcxx-commits
mailing list