From 1de2f2a49cc0d20860f675d426221872db40c51f Mon Sep 17 00:00:00 2001 From: The Phantom Derpstorm Date: Wed, 13 Feb 2019 09:00:07 -0500 Subject: [PATCH] [ constructors ] prevent empty optional access optional types being taken in were forwarding their values without checking, resulting in segfaults on optional -> optional conversions. This fixes those problems in the constructors. --- tl/optional.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tl/optional.hpp b/tl/optional.hpp index 395c58a..8212923 100644 --- a/tl/optional.hpp +++ b/tl/optional.hpp @@ -1190,7 +1190,9 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> optional(const optional &rhs) { - this->construct(*rhs); + if (rhs.has_value()) { + this->construct(*rhs); + } } /// \exclude @@ -1198,7 +1200,9 @@ public: detail::enable_if_t::value> * = nullptr> explicit optional(const optional &rhs) { - this->construct(*rhs); + if (rhs.has_value()) { + this->construct(*rhs); + } } /// Converting move constructor. @@ -1207,7 +1211,9 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> optional(optional &&rhs) { - this->construct(std::move(*rhs)); + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } } /// \exclude @@ -1215,7 +1221,9 @@ public: class U, detail::enable_from_other * = nullptr, detail::enable_if_t::value> * = nullptr> explicit optional(optional &&rhs) { - this->construct(std::move(*rhs)); + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } } /// Destroys the stored value if there is one.