From 4f200def9c735c97a0a2a2571ad455d3af644aa9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 5 Apr 2020 11:32:30 +0200 Subject: [PATCH] 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 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. --- AK/NonnullOwnPtr.h | 2 +- AK/OwnPtr.h | 11 +++++++++-- Libraries/LibIPC/ServerConnection.h | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index c34b09de2fd..ddf2c6e50d7 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -62,7 +62,7 @@ public: template RETURN_TYPESTATE(unconsumed) NonnullOwnPtr(NonnullOwnPtr&& other) - : m_ptr(static_cast(other.leak_ptr())) + : m_ptr(other.leak_ptr()) { ASSERT(m_ptr); } diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index b975abd8739..ed351be0600 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -45,12 +45,12 @@ public: template OwnPtr(NonnullOwnPtr&& other) - : m_ptr(static_cast(other.leak_ptr())) + : m_ptr(other.leak_ptr()) { } template OwnPtr(OwnPtr&& other) - : m_ptr(static_cast(other.leak_ptr())) + : m_ptr(other.leak_ptr()) { } OwnPtr(std::nullptr_t) {}; @@ -148,6 +148,13 @@ public: return NonnullOwnPtr(NonnullOwnPtr::Adopt, *leak_ptr()); } + template + NonnullOwnPtr release_nonnull() + { + ASSERT(m_ptr); + return NonnullOwnPtr(NonnullOwnPtr::Adopt, static_cast(*leak_ptr())); + } + T* ptr() { return m_ptr; } const T* ptr() const { return m_ptr; } diff --git a/Libraries/LibIPC/ServerConnection.h b/Libraries/LibIPC/ServerConnection.h index bd8dfad71bd..949a0787442 100644 --- a/Libraries/LibIPC/ServerConnection.h +++ b/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(); } } 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(); } } }