From b80dc504958e116f40ac6e8ab827be57a30aa4b0 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Fri, 25 Mar 2022 03:47:43 -0700
Subject: Protect intrusive_ptr and ComPtr from moving to itself

---
 common/intrusive_ptr.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

(limited to 'common/intrusive_ptr.h')

diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h
index e1fc1f7b..31afa70f 100644
--- a/common/intrusive_ptr.h
+++ b/common/intrusive_ptr.h
@@ -1,6 +1,8 @@
 #ifndef INTRUSIVE_PTR_H
 #define INTRUSIVE_PTR_H
 
+#include <utility>
+
 #include "atomic.h"
 #include "opthelpers.h"
 
@@ -67,10 +69,13 @@ public:
     }
     intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept
     {
-        if(mPtr)
-            mPtr->release();
-        mPtr = rhs.mPtr;
-        rhs.mPtr = nullptr;
+        if(likely(&rhs != this))
+        {
+            if(mPtr)
+                mPtr->release();
+            mPtr = rhs.mPtr;
+            rhs.mPtr = nullptr;
+        }
         return *this;
     }
 
@@ -87,12 +92,7 @@ public:
         mPtr = ptr;
     }
 
-    T* release() noexcept
-    {
-        T *ret{mPtr};
-        mPtr = nullptr;
-        return ret;
-    }
+    T* release() noexcept { return std::exchange(mPtr, nullptr); }
 
     void swap(intrusive_ptr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
     void swap(intrusive_ptr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
-- 
cgit v1.2.3