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 e0c5625084a93ca391b80b5dac8395d6029ad552..ea15414858e68b5deb0f8f66539c06cc0e824981 100644 (file)
@@ -1070,8 +1070,11 @@ public:
 
   // Move assignment
   basic_fbstring& operator=(basic_fbstring&& goner) {
 
   // 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
     // No need of this anymore
     this->~basic_fbstring();
     // Move the goner into this
index e6b021166d77eb6dca739b236fe84f9959d7a474..bd6def424571cff4b19d849cbb6e9bd11ea1e50b 100644 (file)
@@ -1015,11 +1015,15 @@ TEST(FBString, testFixedBugs) {
       cp += "bb";
     }
   }
       cp += "bb";
     }
   }
-  {
-    // D661622
+  { // D661622
     folly::basic_fbstring<wchar_t> s;
     EXPECT_EQ(0, s.size());
   }
     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());
+  }
 }
 
 
 }