mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-06 16:09:30 +00:00
AK: Disallow constness laundering in RefPtr and NonnullRefPtr
Until now, it was possible to assign a RP<T const> or NNRP<T const> to RP<T> or NNRP<T>. This meant that the constness of the T was lost. We had a lot of code that relied on this sloppiness, and by the time you see this commit, I hopefully found and fixed all of it. :^)
This commit is contained in:
parent
33e87d1627
commit
3c7a0ef1ac
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -47,16 +47,16 @@ public:
|
||||||
|
|
||||||
enum AdoptTag { Adopt };
|
enum AdoptTag { Adopt };
|
||||||
|
|
||||||
ALWAYS_INLINE NonnullRefPtr(T const& object)
|
ALWAYS_INLINE NonnullRefPtr(T& object)
|
||||||
: m_ptr(const_cast<T*>(&object))
|
: m_ptr(&object)
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
ALWAYS_INLINE NonnullRefPtr(U const& object)
|
ALWAYS_INLINE NonnullRefPtr(U& object)
|
||||||
requires(IsConvertible<U*, T*>)
|
requires(IsConvertible<U*, T*>)
|
||||||
: m_ptr(const_cast<T*>(static_cast<T const*>(&object)))
|
: m_ptr(static_cast<T*>(&object))
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other)
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr const& other)
|
||||||
: m_ptr(const_cast<T*>(other.ptr()))
|
: m_ptr(other.ptr())
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ public:
|
||||||
template<typename U>
|
template<typename U>
|
||||||
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U> const& other)
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U> const& other)
|
||||||
requires(IsConvertible<U*, T*>)
|
requires(IsConvertible<U*, T*>)
|
||||||
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
: m_ptr(static_cast<T*>(other.ptr()))
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -145,7 +145,7 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtr& operator=(T const& object)
|
NonnullRefPtr& operator=(T& object)
|
||||||
{
|
{
|
||||||
NonnullRefPtr tmp { object };
|
NonnullRefPtr tmp { object };
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
|
|
24
AK/RefPtr.h
24
AK/RefPtr.h
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -35,14 +35,14 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
RefPtr() = default;
|
RefPtr() = default;
|
||||||
RefPtr(T const* ptr)
|
RefPtr(T* ptr)
|
||||||
: m_ptr(const_cast<T*>(ptr))
|
: m_ptr(ptr)
|
||||||
{
|
{
|
||||||
ref_if_not_null(m_ptr);
|
ref_if_not_null(m_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr(T const& object)
|
RefPtr(T& object)
|
||||||
: m_ptr(const_cast<T*>(&object))
|
: m_ptr(&object)
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE RefPtr(NonnullRefPtr<T> const& other)
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<T> const& other)
|
||||||
: m_ptr(const_cast<T*>(other.ptr()))
|
: m_ptr(other.ptr())
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ public:
|
||||||
template<typename U>
|
template<typename U>
|
||||||
ALWAYS_INLINE RefPtr(NonnullRefPtr<U> const& other)
|
ALWAYS_INLINE RefPtr(NonnullRefPtr<U> const& other)
|
||||||
requires(IsConvertible<U*, T*>)
|
requires(IsConvertible<U*, T*>)
|
||||||
: m_ptr(const_cast<T*>(static_cast<T const*>(other.ptr())))
|
: m_ptr(static_cast<T*>(other.ptr()))
|
||||||
{
|
{
|
||||||
m_ptr->ref();
|
m_ptr->ref();
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ public:
|
||||||
template<typename U>
|
template<typename U>
|
||||||
RefPtr(RefPtr<U> const& other)
|
RefPtr(RefPtr<U> const& other)
|
||||||
requires(IsConvertible<U*, T*>)
|
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);
|
ref_if_not_null(m_ptr);
|
||||||
}
|
}
|
||||||
|
@ -181,14 +181,14 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE RefPtr& operator=(T const* ptr)
|
ALWAYS_INLINE RefPtr& operator=(T* ptr)
|
||||||
{
|
{
|
||||||
RefPtr tmp { ptr };
|
RefPtr tmp { ptr };
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ALWAYS_INLINE RefPtr& operator=(T const& object)
|
ALWAYS_INLINE RefPtr& operator=(T& object)
|
||||||
{
|
{
|
||||||
RefPtr tmp { object };
|
RefPtr tmp { object };
|
||||||
swap(tmp);
|
swap(tmp);
|
||||||
|
@ -304,13 +304,13 @@ struct Traits<RefPtr<T>> : public GenericTraits<RefPtr<T>> {
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
inline NonnullRefPtr<T> static_ptr_cast(NonnullRefPtr<U> const& ptr)
|
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>
|
template<typename T, typename U>
|
||||||
inline RefPtr<T> static_ptr_cast(RefPtr<U> const& ptr)
|
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, typename U>
|
template<typename T, typename U>
|
||||||
|
|
Loading…
Reference in a new issue