AK: Disallow constness laundering in RefPtr and NonnullRefPtr

This is a re-application of 3c7a0ef1ac

Co-Authored-By: Andreas Kling <andreas@ladybird.org>
This commit is contained in:
Andrew Kaster 2025-04-15 16:05:25 -06:00 committed by Andrew Kaster
parent 5e1b3cdeb9
commit ad00306daf
Notes: github-actions[bot] 2025-04-16 16:42:48 +00:00
2 changed files with 18 additions and 18 deletions

View file

@ -46,16 +46,16 @@ public:
enum AdoptTag { Adopt };
ALWAYS_INLINE NonnullRefPtr(T const& object)
: m_ptr(const_cast<T*>(&object))
ALWAYS_INLINE NonnullRefPtr(T& object)
: m_ptr(&object)
{
m_ptr->ref();
}
template<typename U>
requires(IsConvertible<U*, T*>)
ALWAYS_INLINE NonnullRefPtr(U const& object)
: m_ptr(const_cast<T*>(static_cast<T const*>(&object)))
ALWAYS_INLINE NonnullRefPtr(U& object)
: m_ptr(static_cast<T*>(&object))
{
m_ptr->ref();
}
@ -78,7 +78,7 @@ public:
}
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other)
: m_ptr(const_cast<T*>(other.ptr()))
: m_ptr(other.ptr())
{
m_ptr->ref();
}
@ -86,7 +86,7 @@ public:
template<typename U>
requires(IsConvertible<U*, T*>)
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U> const& other)
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
: m_ptr(static_cast<T*>(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);

View file

@ -34,14 +34,14 @@ public:
};
RefPtr() = default;
RefPtr(T const* ptr)
: m_ptr(const_cast<T*>(ptr))
RefPtr(T* ptr)
: m_ptr(ptr)
{
ref_if_not_null(m_ptr);
}
RefPtr(T const& object)
: m_ptr(const_cast<T*>(&object))
RefPtr(T& object)
: m_ptr(&object)
{
m_ptr->ref();
}
@ -57,7 +57,7 @@ public:
}
ALWAYS_INLINE RefPtr(NonnullRefPtr<T> const& other)
: m_ptr(const_cast<T*>(other.ptr()))
: m_ptr(other.ptr())
{
m_ptr->ref();
}
@ -65,7 +65,7 @@ public:
template<typename U>
requires(IsConvertible<U*, T*>)
ALWAYS_INLINE RefPtr(NonnullRefPtr<U> const& other)
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
: m_ptr(static_cast<T*>(other.ptr()))
{
m_ptr->ref();
}
@ -93,7 +93,7 @@ public:
template<typename U>
RefPtr(RefPtr<U> const& other)
requires(IsConvertible<U*, T*>)
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
: m_ptr(static_cast<T*>(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<RefPtr<T>> : public DefaultTraits<RefPtr<T>> {
template<typename T, typename U>
inline NonnullRefPtr<T> static_ptr_cast(NonnullRefPtr<U> const& ptr)
{
return NonnullRefPtr<T>(static_cast<T const&>(*ptr));
return NonnullRefPtr<T>(static_cast<T&>(*ptr));
}
template<typename T, typename U>
inline RefPtr<T> static_ptr_cast(RefPtr<U> const& ptr)
{
return RefPtr<T>(static_cast<T const*>(ptr.ptr()));
return RefPtr<T>(static_cast<T*>(ptr.ptr()));
}
template<typename T>