From 151e6a18185579b9753b3712785b3c0399ea9899 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 5 Aug 2019 22:27:47 +0200 Subject: [PATCH] AK: Fix leak in Optional(Optional&&) We were *copying* the other Optional's value and then marking it as not having a value. This prevented us from ever destroying the original. --- AK/Optional.h | 2 +- AK/Tests/TestOptional.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/AK/Optional.h b/AK/Optional.h index a73db53bcc..f53176e0e7 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -29,7 +29,7 @@ public: : m_has_value(other.m_has_value) { if (m_has_value) { - new (&m_storage) T(move(other.value_without_consume_state())); + new (&m_storage) T(other.release_value()); other.m_has_value = false; } } diff --git a/AK/Tests/TestOptional.cpp b/AK/Tests/TestOptional.cpp index 02db877cea..5ca2fe4dfc 100644 --- a/AK/Tests/TestOptional.cpp +++ b/AK/Tests/TestOptional.cpp @@ -27,4 +27,17 @@ TEST_CASE(move_optional) EXPECT_EQ(x.has_value(), false); } +TEST_CASE(optional_leak_1) +{ + struct Structure { + Optional str; + }; + + // This used to leak, it does not anymore. + Vector vec; + vec.append({ "foo" }); + EXPECT_EQ(vec[0].str.has_value(), true); + EXPECT_EQ(vec[0].str.value(), "foo"); +} + TEST_MAIN(Optional)