fix Expected<..., eUnion> move / copy ctor
authorPhilip Pronin <philipp@fb.com>
Tue, 25 Oct 2016 06:27:16 +0000 (23:27 -0700)
committerFacebook Github Bot <facebook-github-bot-bot@fb.com>
Tue, 25 Oct 2016 06:38:29 +0000 (23:38 -0700)
Summary:
Consider `Expected<..., eUnion>(Expected&&)` ctor, where
`ExpectedUnion` move ctor would be called first, which is noop leaving
`which_`  uninitialized; then `MoveConstructible` ctor is executed, calling
`assignValue()`, which performs action depending on uninitialized `which_`.

Reviewed By: yfeldblum

Differential Revision: D4073199

fbshipit-source-id: 623660e7047afcebf9f72d83f91f84ff6078090f

folly/Expected.h

index 3de0a6d9b4230ef6d53408966ea701cca57c628f..b4e63ee9dc5222d2c996128c4f9e7163429f87fe 100644 (file)
@@ -249,12 +249,11 @@ struct ExpectedUnion {
   union {
     Value value_;
     Error error_;
-    char ch_;
+    char ch_{};
   };
-  Which which_;
+  Which which_ = Which::eEmpty;
 
-  explicit constexpr ExpectedUnion(EmptyTag) noexcept
-      : ch_{}, which_(Which::eEmpty) {}
+  explicit constexpr ExpectedUnion(EmptyTag) noexcept {}
   template <class... Vs>
   explicit constexpr ExpectedUnion(ValueTag, Vs&&... vs) noexcept(
       noexcept(Value(static_cast<Vs&&>(vs)...)))