From 70576ca592a5f27bfc2a12112ad968bf538f4ffc Mon Sep 17 00:00:00 2001
From: thorstenhater <24411438+thorstenhater@users.noreply.github.com>
Date: Thu, 6 Aug 2020 02:04:09 +0200
Subject: [PATCH] Fix issues found in device_coordinator.hpp. (#1086)

* Check for self-assignment in `device_reference::operator=(const device_reference&)`.
* Return reference to self in ` device_reference::operator=(const device_reference&)`.
* Prohibit default construction of `device_reference` and clarify copy semantics.
---
 arbor/memory/device_coordinator.hpp | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/arbor/memory/device_coordinator.hpp b/arbor/memory/device_coordinator.hpp
index f9b3c864..ab8733d7 100644
--- a/arbor/memory/device_coordinator.hpp
+++ b/arbor/memory/device_coordinator.hpp
@@ -80,10 +80,19 @@ public:
         return *this;
     }
 
+    // Assigning to a reference will copy the referenced memory
     device_reference& operator=(const device_reference& ref) {
-        gpu_memcpy_d2d(pointer_, ref.pointer_, sizeof(T));
+        if (this != &ref) {
+            gpu_memcpy_d2d(pointer_, ref.pointer_, sizeof(T));
+        }
+        return *this;
     }
 
+    // No empty references
+    device_reference() = delete;
+    // Copying a reference is a shallow copy
+    device_reference(const device_reference& ref) = default;
+
     operator T() const {
         T tmp;
         gpu_memcpy_d2h(&tmp, pointer_, sizeof(T));
-- 
GitLab