Skip to content

Commit bbc637a

Browse files
remove UB using list
1 parent 2f689f9 commit bbc637a

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

libcxx/include/__tree

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,25 @@ class _LIBCPP_STANDALONE_DEBUG __tree_node : public __tree_node_base<_VoidPtr> {
582582
public:
583583
using __node_value_type _LIBCPP_NODEBUG = __get_node_value_type_t<_Tp>;
584584

585-
union {
586-
__node_value_type __value_;
587-
};
585+
// We allow starting the lifetime of nodes without initializing the value held by the node,
586+
// since that is handled by the map itself in order to be allocator-aware.
587+
# ifndef _LIBCPP_CXX03_LANG
588+
589+
private:
590+
union {
591+
__node_value_type __value_;
592+
};
593+
594+
public:
595+
_LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return __value_; }
596+
# else
597+
598+
private:
599+
_ALIGNAS_TYPE(__node_value_type) unsigned char __buffer_[sizeof(__node_value_type)];
588600

589-
_LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return __value_; }
601+
public:
602+
_LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return *std::__launder(reinterpret_cast<_Tp*>(&__buffer_)); }
603+
# endif
590604

591605
template <class _Alloc, class... _Args>
592606
explicit __tree_node(_Alloc& __na, _Args&&... __args) {

0 commit comments

Comments
 (0)