Browse Source

AK: Stop allowing implicit downcast with OwnPtr and NonnullOwnPtr

Same issue here as we had with RefPtr and NonnullRefPtr.

Since we can't make copies of an owning pointer, we don't get quite the
same static_ptr_cast<T> here. Instead I've only added a new templated
version of OwnPtr::release_nonnull() in this patch, to solve the only
issue that popped up.

I'm not sure what the best solution here is, but this works for now.
Andreas Kling 5 years ago
parent
commit
4f200def9c
3 changed files with 12 additions and 5 deletions
  1. 1 1
      AK/NonnullOwnPtr.h
  2. 9 2
      AK/OwnPtr.h
  3. 2 2
      Libraries/LibIPC/ServerConnection.h

+ 1 - 1
AK/NonnullOwnPtr.h

@@ -62,7 +62,7 @@ public:
     template<typename U>
     RETURN_TYPESTATE(unconsumed)
     NonnullOwnPtr(NonnullOwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
         ASSERT(m_ptr);
     }

+ 9 - 2
AK/OwnPtr.h

@@ -45,12 +45,12 @@ public:
 
     template<typename U>
     OwnPtr(NonnullOwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
     }
     template<typename U>
     OwnPtr(OwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
     }
     OwnPtr(std::nullptr_t) {};
@@ -148,6 +148,13 @@ public:
         return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *leak_ptr());
     }
 
+    template<typename U>
+    NonnullOwnPtr<U> release_nonnull()
+    {
+        ASSERT(m_ptr);
+        return NonnullOwnPtr<U>(NonnullOwnPtr<U>::Adopt, static_cast<U&>(*leak_ptr()));
+    }
+
     T* ptr() { return m_ptr; }
     const T* ptr() const { return m_ptr; }
 

+ 2 - 2
Libraries/LibIPC/ServerConnection.h

@@ -93,7 +93,7 @@ public:
             if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
                 auto message = move(m_unprocessed_messages[i]);
                 m_unprocessed_messages.remove(i);
-                return message;
+                return message.template release_nonnull<MessageType>();
             }
         }
         for (;;) {
@@ -112,7 +112,7 @@ public:
                 if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
                     auto message = move(m_unprocessed_messages[i]);
                     m_unprocessed_messages.remove(i);
-                    return message;
+                    return message.template release_nonnull<MessageType>();
                 }
             }
         }