fix fbstring move assignment operator
authorPhilip Pronin <philipp@fb.com>
Tue, 23 Apr 2013 06:36:47 +0000 (23:36 -0700)
committerSara Golemon <sgolemon@fb.com>
Mon, 20 May 2013 18:01:26 +0000 (11:01 -0700)
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

folly/FBString.h
folly/test/FBStringTest.cpp

index e0c5625..ea15414 100644 (file)
@@ -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
index e6b0211..bd6def4 100644 (file)
@@ -1015,11 +1015,15 @@ TEST(FBString, testFixedBugs) {
       cp += "bb";
     }
   }
-  {
-    // D661622
+  { // D661622
     folly::basic_fbstring<wchar_t> s;
     EXPECT_EQ(0, s.size());
   }
+  { // D785057
+    fbstring str(1337, 'f');
+    std::swap(str, str);
+    EXPECT_EQ(1337, str.size());
+  }
 }