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 275403 36a0d0731ca6282f2a44b66f32eba4ff049ef2e4
parent 275402 4febcc97a5d6b522c2b2bed36bf366b09e46b0a1
child 275404 5e733820ad95f3abed5396fa33feedff407a457b
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8, abillings
bugs1182723
milestone40.0
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
@@ -869,27 +869,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
   {