From ad00306daf232063f9fb81affcee7cba70516db2 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Tue, 15 Apr 2025 16:05:25 -0600 Subject: [PATCH] AK: Disallow constness laundering in RefPtr and NonnullRefPtr This is a re-application of 3c7a0ef1ac279c99bc4ee72085278ea70c210889 Co-Authored-By: Andreas Kling --- AK/NonnullRefPtr.h | 14 +++++++------- AK/RefPtr.h | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 92df14c462c..762a989cb43 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -46,16 +46,16 @@ public: enum AdoptTag { Adopt }; - ALWAYS_INLINE NonnullRefPtr(T const& object) - : m_ptr(const_cast(&object)) + ALWAYS_INLINE NonnullRefPtr(T& object) + : m_ptr(&object) { m_ptr->ref(); } template requires(IsConvertible) - ALWAYS_INLINE NonnullRefPtr(U const& object) - : m_ptr(const_cast(static_cast(&object))) + ALWAYS_INLINE NonnullRefPtr(U& object) + : m_ptr(static_cast(&object)) { m_ptr->ref(); } @@ -78,7 +78,7 @@ public: } ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other) - : m_ptr(const_cast(other.ptr())) + : m_ptr(other.ptr()) { m_ptr->ref(); } @@ -86,7 +86,7 @@ public: template requires(IsConvertible) ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other) - : m_ptr(const_cast(static_cast(other.ptr()))) + : m_ptr(static_cast(other.ptr())) { m_ptr->ref(); } @@ -144,7 +144,7 @@ public: return *this; } - NonnullRefPtr& operator=(T const& object) + NonnullRefPtr& operator=(T& object) { NonnullRefPtr tmp { object }; swap(tmp); diff --git a/AK/RefPtr.h b/AK/RefPtr.h index ad0199b47f6..d02ace7e4fe 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -34,14 +34,14 @@ public: }; RefPtr() = default; - RefPtr(T const* ptr) - : m_ptr(const_cast(ptr)) + RefPtr(T* ptr) + : m_ptr(ptr) { ref_if_not_null(m_ptr); } - RefPtr(T const& object) - : m_ptr(const_cast(&object)) + RefPtr(T& object) + : m_ptr(&object) { m_ptr->ref(); } @@ -57,7 +57,7 @@ public: } ALWAYS_INLINE RefPtr(NonnullRefPtr const& other) - : m_ptr(const_cast(other.ptr())) + : m_ptr(other.ptr()) { m_ptr->ref(); } @@ -65,7 +65,7 @@ public: template requires(IsConvertible) ALWAYS_INLINE RefPtr(NonnullRefPtr const& other) - : m_ptr(const_cast(static_cast(other.ptr()))) + : m_ptr(static_cast(other.ptr())) { m_ptr->ref(); } @@ -93,7 +93,7 @@ public: template RefPtr(RefPtr const& other) requires(IsConvertible) - : m_ptr(const_cast(static_cast(other.ptr()))) + : m_ptr(static_cast(other.ptr())) { ref_if_not_null(m_ptr); } @@ -180,14 +180,14 @@ public: return *this; } - ALWAYS_INLINE RefPtr& operator=(T const* ptr) + ALWAYS_INLINE RefPtr& operator=(T* ptr) { RefPtr tmp { ptr }; swap(tmp); return *this; } - ALWAYS_INLINE RefPtr& operator=(T const& object) + ALWAYS_INLINE RefPtr& operator=(T& object) { RefPtr tmp { object }; swap(tmp); @@ -303,13 +303,13 @@ struct Traits> : public DefaultTraits> { template inline NonnullRefPtr static_ptr_cast(NonnullRefPtr const& ptr) { - return NonnullRefPtr(static_cast(*ptr)); + return NonnullRefPtr(static_cast(*ptr)); } template inline RefPtr static_ptr_cast(RefPtr const& ptr) { - return RefPtr(static_cast(ptr.ptr())); + return RefPtr(static_cast(ptr.ptr())); } template