aboutsummaryrefslogtreecommitdiffstats
path: root/common/intrusive_ptr.h
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-03-25 03:47:43 -0700
committerChris Robinson <[email protected]>2022-03-25 03:47:43 -0700
commitb80dc504958e116f40ac6e8ab827be57a30aa4b0 (patch)
treeca73c49b0df01352163d104d0ab2d50c0fc63c0d /common/intrusive_ptr.h
parent1ee34556a63217ecb49f4986ff6b759377535dc4 (diff)
Protect intrusive_ptr and ComPtr from moving to itself
Diffstat (limited to 'common/intrusive_ptr.h')
-rw-r--r--common/intrusive_ptr.h20
1 files changed, 10 insertions, 10 deletions
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); }