Browse Source

Guard against self-assignment clearing the containers.

Lasse Öörni 9 years ago
parent
commit
2d59e72df7

+ 6 - 2
Source/Urho3D/Container/HashMap.h

@@ -262,8 +262,12 @@ public:
     /// Assign a hash map.
     /// Assign a hash map.
     HashMap& operator =(const HashMap<T, U>& rhs)
     HashMap& operator =(const HashMap<T, U>& rhs)
     {
     {
-        Clear();
-        Insert(rhs);
+        // In case of self-assignment do nothing
+        if (&rhs != this)
+        {
+            Clear();
+            Insert(rhs);
+        }
         return *this;
         return *this;
     }
     }
 
 

+ 6 - 2
Source/Urho3D/Container/HashSet.h

@@ -217,8 +217,12 @@ public:
     /// Assign a hash set.
     /// Assign a hash set.
     HashSet& operator =(const HashSet<T>& rhs)
     HashSet& operator =(const HashSet<T>& rhs)
     {
     {
-        Clear();
-        Insert(rhs);
+        // In case of self-assignment do nothing
+        if (&rhs != this)
+        {
+            Clear();
+            Insert(rhs);
+        }
         return *this;
         return *this;
     }
     }
 
 

+ 6 - 3
Source/Urho3D/Container/List.h

@@ -209,9 +209,12 @@ public:
     /// Assign from another list.
     /// Assign from another list.
     List& operator =(const List<T>& rhs)
     List& operator =(const List<T>& rhs)
     {
     {
-        // Clear, then insert the nodes of the other list
-        Clear();
-        Insert(End(), rhs);
+        // Clear, then insert the nodes of the other list. In case of self-assignment do nothing
+        if (&rhs != this)
+        {
+            Clear();
+            Insert(End(), rhs);
+        }
         return *this;
         return *this;
     }
     }
 
 

+ 12 - 4
Source/Urho3D/Container/Vector.h

@@ -97,8 +97,12 @@ public:
     /// Assign from another vector.
     /// Assign from another vector.
     Vector<T>& operator =(const Vector<T>& rhs)
     Vector<T>& operator =(const Vector<T>& rhs)
     {
     {
-        Clear();
-        InsertElements(0, rhs.Begin(), rhs.End());
+        // In case of self-assignment do nothing
+        if (&rhs != this)
+        {
+            Clear();
+            InsertElements(0, rhs.Begin(), rhs.End());
+        }
         return *this;
         return *this;
     }
     }
 
 
@@ -614,8 +618,12 @@ public:
     /// Assign from another vector.
     /// Assign from another vector.
     PODVector<T>& operator =(const PODVector<T>& rhs)
     PODVector<T>& operator =(const PODVector<T>& rhs)
     {
     {
-        Clear();
-        InsertElements(0, rhs.Begin(), rhs.End());
+        // In case of self-assignment do nothing
+        if (&rhs != this)
+        {
+            Clear();
+            InsertElements(0, rhs.Begin(), rhs.End());
+        }
         return *this;
         return *this;
     }
     }