瀏覽代碼

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 年之前
父節點
當前提交
4f200def9c
共有 3 個文件被更改,包括 12 次插入5 次删除
  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>
     template<typename U>
     RETURN_TYPESTATE(unconsumed)
     RETURN_TYPESTATE(unconsumed)
     NonnullOwnPtr(NonnullOwnPtr<U>&& other)
     NonnullOwnPtr(NonnullOwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
     {
         ASSERT(m_ptr);
         ASSERT(m_ptr);
     }
     }

+ 9 - 2
AK/OwnPtr.h

@@ -45,12 +45,12 @@ public:
 
 
     template<typename U>
     template<typename U>
     OwnPtr(NonnullOwnPtr<U>&& other)
     OwnPtr(NonnullOwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
     {
     }
     }
     template<typename U>
     template<typename U>
     OwnPtr(OwnPtr<U>&& other)
     OwnPtr(OwnPtr<U>&& other)
-        : m_ptr(static_cast<T*>(other.leak_ptr()))
+        : m_ptr(other.leak_ptr())
     {
     {
     }
     }
     OwnPtr(std::nullptr_t) {};
     OwnPtr(std::nullptr_t) {};
@@ -148,6 +148,13 @@ public:
         return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *leak_ptr());
         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; }
     T* ptr() { return m_ptr; }
     const T* ptr() const { 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()) {
             if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
                 auto message = move(m_unprocessed_messages[i]);
                 auto message = move(m_unprocessed_messages[i]);
                 m_unprocessed_messages.remove(i);
                 m_unprocessed_messages.remove(i);
-                return message;
+                return message.template release_nonnull<MessageType>();
             }
             }
         }
         }
         for (;;) {
         for (;;) {
@@ -112,7 +112,7 @@ public:
                 if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
                 if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
                     auto message = move(m_unprocessed_messages[i]);
                     auto message = move(m_unprocessed_messages[i]);
                     m_unprocessed_messages.remove(i);
                     m_unprocessed_messages.remove(i);
-                    return message;
+                    return message.template release_nonnull<MessageType>();
                 }
                 }
             }
             }
         }
         }