From: Philip Pronin Date: Tue, 23 Apr 2013 06:36:47 +0000 (-0700) Subject: fix fbstring move assignment operator X-Git-Tag: v0.22.0~998 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=4b9cc255b07960d87abe202ab898ad5bb5eba7ca;p=folly.git fix fbstring move assignment operator Summary: 21.4.2 [string.cons] / 23 says > If *this and str are the same object, the member has no effect. That means we have to support self-move-assignment. Test Plan: added test which triggered assertion, ran it Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D785057 --- diff --git a/folly/FBString.h b/folly/FBString.h index e0c56250..ea154148 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -1070,8 +1070,11 @@ public: // Move assignment basic_fbstring& operator=(basic_fbstring&& goner) { - // Self move assignment is illegal, see 17.6.4.9 for the explanation - assert(&goner != this); + if (FBSTRING_UNLIKELY(&goner == this)) { + // Compatibility with std::basic_string<>, + // 21.4.2 [string.cons] / 23 requires self-move-assignment support. + return *this; + } // No need of this anymore this->~basic_fbstring(); // Move the goner into this diff --git a/folly/test/FBStringTest.cpp b/folly/test/FBStringTest.cpp index e6b02116..bd6def42 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -1015,11 +1015,15 @@ TEST(FBString, testFixedBugs) { cp += "bb"; } } - { - // D661622 + { // D661622 folly::basic_fbstring s; EXPECT_EQ(0, s.size()); } + { // D785057 + fbstring str(1337, 'f'); + std::swap(str, str); + EXPECT_EQ(1337, str.size()); + } }