[PATCH] [libcxx] Attempt to fix undefined behavior in list, forward_list and __tree.

Richard Smith richard at metafoo.co.uk
Wed Jan 14 14:01:35 PST 2015

It seems to me that the right thing to do is to fix `__tree` and `__tree_iterator` to use the appropriate pointer type for all stored pointers. That is,  `__tree_node_base::__right_`, `__tree_iterator::__ptr_`,  `__tree_const_iterator::__ptr_`, and `__tree::__begin_node_` should be pointers to `__tree_end_node`, because they might point to a `__tree_end_node` that is not a `__tree_node_base`.

As-is, the code *still* has undefined behavior in the case where the element type has a higher alignment requirement than that of a pointer, because you will potentially use a `node_pointer` to point to an element that is not suitably aligned to be represented as such a pointer value. This problem doesn't arise if you only use a pointer-to-`T` type to point at a `T` object.

Comment at: include/memory:5452-5458
@@ +5451,9 @@
+  {
+    return static_cast<_To>(_FromPtrTraits::pointer_to(__e));
+  }
+  static _To __from_pointer(_From & __f)
+  {
+    return static_cast<_To>(__f);
+  }
It seems strange to use `static_cast` here and `reinterpret_cast` below. Even assuming the `reinterpret_cast` approach works, don't we still have the same problem if the user uses a fancy pointer type?



More information about the cfe-commits mailing list