[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
Fri Oct 27 12:30:56 PDT 2023


================
@@ -102,6 +104,44 @@ struct TrackedMove {
   }
 };
 
+// This type has one byte of tail padding where `std::expected` may put its
+// "has value" flag. The constructor will clobber all bytes including the
+// tail padding. With this type we can check that `std::expected` handles
+// the case where the "has value" flag is an overlapping subobject correctly.
+//
+// See https://github.com/llvm/llvm-project/issues/68552 for details.
+template <int constant>
+struct TailClobberer {
+  constexpr TailClobberer() noexcept {
+    if (!std::is_constant_evaluated()) {
+      std::memset(this, constant, sizeof(*this));
+    }
+    // Always set `b` itself to `false` so that the comparison works.
+    b = false;
+  }
+  constexpr TailClobberer(const TailClobberer&) : TailClobberer() {}
----------------
jiixyj wrote:

Is it important for the `TailClobberer` to be trivially copyable? My intention was to have the copy constructor also clobber to catch potential issues in `expected`'s non-trivial copy constructor.

https://github.com/llvm/llvm-project/pull/68733


More information about the libcxx-commits mailing list