From ce4d7e9d0e6bb5c761d41b9bdfbad0624dcbb994 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Mon, 16 Oct 2017 11:15:46 +0200 Subject: [PATCH] Utils: Fix smallstring move assignment Before the string was simply swapped with the other string which can lead to an unexpected behavior for xvalues. Now the destructor of the source is called and it is default initialized. foo = std::move(bar); bar would now hold the value of foo. Change-Id: Ibea3f18333a168634b7faf2fdaf9b5b52c82d5cc Reviewed-by: Tim Jenssen --- src/libs/utils/smallstring.h | 7 +++++-- tests/unit/unittest/smallstring-test.cpp | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h index f3586092582..02c18a2c60d 100644 --- a/src/libs/utils/smallstring.h +++ b/src/libs/utils/smallstring.h @@ -172,14 +172,17 @@ public: } BasicSmallString(BasicSmallString &&other) noexcept + : m_data(other.m_data) { - m_data = other.m_data; other.m_data = Internal::StringDataLayout(); } BasicSmallString &operator=(BasicSmallString &&other) noexcept { - swap(*this, other); + this->~BasicSmallString(); + + m_data = other.m_data; + other.m_data = Internal::StringDataLayout(); return *this; } diff --git a/tests/unit/unittest/smallstring-test.cpp b/tests/unit/unittest/smallstring-test.cpp index bee07f58ffa..ca6534b558f 100644 --- a/tests/unit/unittest/smallstring-test.cpp +++ b/tests/unit/unittest/smallstring-test.cpp @@ -1495,7 +1495,7 @@ TEST(SmallString, ShortSmallStringMoveAssignment) copy = std::move(text); - ASSERT_THAT(text, SmallString("more text")); + ASSERT_THAT(text, IsEmpty()); ASSERT_THAT(copy, SmallString("text")); } @@ -1506,7 +1506,7 @@ TEST(SmallString, LongSmallStringMoveAssignment) copy = std::move(text); - ASSERT_THAT(text, SmallString("more text")); + ASSERT_THAT(text, IsEmpty()); ASSERT_THAT(copy, SmallString("this is a very very very very long text")); }