Bug 1182723 - Properly handle self-assignment in nsTArray::operator=. r=mccr8, a=abillings
authorBirunthan Mohanathas <birunthan@mohanathas.com>
Tue, 21 Jul 2015 09:42:58 -0700
changeset 281616 e342d50c37d2b557fa8d98b1bffc475ab7233c26
parent 281615 4c0cd1fa55ddaf2069bccc35e3797c3affd3937a
child 281617 eb80ce0eb9c848d862b77613561808dfe3541e35
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8, abillings
bugs1182723
milestone41.0a2
Bug 1182723 - Properly handle self-assignment in nsTArray::operator=. r=mccr8, a=abillings
xpcom/glue/nsTArray.h
--- a/xpcom/glue/nsTArray.h
+++ b/xpcom/glue/nsTArray.h
@@ -877,27 +877,31 @@ public:
     return *reinterpret_cast<const FallibleTArray<E>*>(this);
   }
 
   // The array's assignment operator performs a 'deep' copy of the given
   // array.  It is optimized to reuse existing storage if possible.
   // @param aOther The array object to copy.
   self_type& operator=(const self_type& aOther)
   {
-    ReplaceElementsAt(0, Length(), aOther.Elements(), aOther.Length());
+    if (this != &aOther) {
+      ReplaceElementsAt(0, Length(), aOther.Elements(), aOther.Length());
+    }
     return *this;
   }
 
   // The array's move assignment operator steals the underlying data from
   // the other array.
   // @param other  The array object to move from.
   self_type& operator=(self_type&& aOther)
   {
-    Clear();
-    SwapElements(aOther);
+    if (this != &aOther) {
+      Clear();
+      SwapElements(aOther);
+    }
     return *this;
   }
 
   // Return true if this array has the same length and the same
   // elements as |aOther|.
   template<typename Allocator>
   bool operator==(const nsTArray_Impl<E, Allocator>& aOther) const
   {