]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/base/CbcPointer.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / base / CbcPointer.h
index 62a9463b86b925f9805048c35f2f9b0b3c2f437a..9ae32f2fa6092174168bfe1585bcd595d8ae02b8 100644 (file)
@@ -11,7 +11,7 @@
 /**
  \ingroup CBDATAAPI
  *
- * Safely points to a cbdata-protected class (cbc), such as an AsyncJob. 
+ * Safely points to a cbdata-protected class (cbc), such as an AsyncJob.
  * When a cbc we communicate with disappears without
  * notice or a notice has not reached us yet, this class prevents
  * dereferencing the pointer to the gone cbc object.
@@ -48,10 +48,12 @@ public:
     /// support assigning a child cbc pointer to a parent cbc pointer
     template <typename Other>
     CbcPointer &operator =(const CbcPointer<Other> &o) {
-        clear();
-        cbc = o.raw(); // so that set() is accurate
-        if (o.valid())
-            lock = cbdataReference(o->toCbdata());
+        if (this != &o) { // assignment to self
+            clear();
+            cbc = o.raw(); // so that set() is accurate
+            if (o.valid())
+                lock = cbdataReference(o->toCbdata());
+        }
         return *this;
     }
 
@@ -66,7 +68,8 @@ private:
 
 template <class Cbc>
 inline
-std::ostream &operator <<(std::ostream &os, const CbcPointer<Cbc> &p) {
+std::ostream &operator <<(std::ostream &os, const CbcPointer<Cbc> &p)
+{
     return p.print(os);
 }
 
@@ -100,10 +103,12 @@ CbcPointer<Cbc>::~CbcPointer()
 template<class Cbc>
 CbcPointer<Cbc> &CbcPointer<Cbc>::operator =(const CbcPointer &d)
 {
-    clear();
-    cbc = d.cbc;
-    if (d.lock && cbdataReferenceValid(d.lock))
-        lock = cbdataReference(d.lock);
+    if (this != &d) { // assignment to self
+        clear();
+        cbc = d.cbc;
+        if (d.lock && cbdataReferenceValid(d.lock))
+            lock = cbdataReference(d.lock);
+    }
     return *this;
 }
 
@@ -148,9 +153,9 @@ CbcPointer<Cbc>::operator ->() const
 }
 
 template <class Cbc>
-std::ostream &CbcPointer<Cbc>::print(std::ostream &os) const {
+std::ostream &CbcPointer<Cbc>::print(std::ostream &os) const
+{
     return os << cbc << '/' << lock;
 }
 
-
 #endif /* SQUID_CBC_POINTER_H */