[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 17 10:56:06 PDT 2023


================
@@ -43,6 +45,38 @@ constexpr bool test() {
     assert(!e.has_value());
   }
 
+  // The following tests check that the "has_value" flag is not overwritten
+  // by the constructor of the value. This could happen because the flag is
+  // stored in the tail padding of the value.
+  //
+  // The first test is a simplified version of the real code where this was
+  // first observed.
+  //
+  // The other tests use a synthetic struct that clobbers its tail padding
+  // on construction, making the issue easier to reproduce.
+  //
+  // See https://github.com/llvm/llvm-project/issues/68552 and the linked PR.
+  {
+#if !defined(TEST_COMPILER_CLANG) || TEST_CLANG_VER >= 1600
----------------
jiixyj wrote:

I noticed that AppleClang 15 failed with the following error:

```
_bk;t=1697554928004# | /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/utilities/expected/expected.expected/observers/has_value.pass.cpp:80:17: error: static assertion expression is not an integral constant expression
_bk;t=1697554928004# |   static_assert(test());
_bk;t=1697554928004# |                 ^~~~~~
_bk;t=1697554928004# | /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/utilities/expected/expected.expected/observers/has_value.pass.cpp:60:27: note: control flows through the definition of a static variable
_bk;t=1697554928004# |     static constexpr auto f1 = [] -> std::expected<std::optional<int>, long> { return 0; };
```

Maybe there is a better way to fix this?

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


More information about the libcxx-commits mailing list