From 15866714da033763ccea4a866a2522d0ba88f1f9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 2 Aug 2019 12:00:43 +0200 Subject: [PATCH] AK: Add anti-null assertions in RefPtr. This gives us better error messages when dereferencing null RefPtrs. --- AK/RefPtr.h | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 70dc84f97f0..d698cfa261a 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -49,18 +49,21 @@ public: RefPtr(const NonnullRefPtr& other) : m_ptr(const_cast(other.ptr())) { + ASSERT(m_ptr); m_ptr->ref(); } template RefPtr(const NonnullRefPtr& other) : m_ptr(static_cast(const_cast(other.ptr()))) { + ASSERT(m_ptr); m_ptr->ref(); } template RefPtr(NonnullRefPtr&& other) : m_ptr(static_cast(&other.leak_ref())) { + ASSERT(m_ptr); } template RefPtr(RefPtr&& other) @@ -121,6 +124,7 @@ public: { RefPtr tmp = move(other); swap(tmp); + ASSERT(m_ptr); return *this; } @@ -128,6 +132,7 @@ public: { RefPtr tmp = other; swap(tmp); + ASSERT(m_ptr); return *this; } @@ -136,6 +141,7 @@ public: { RefPtr tmp = other; swap(tmp); + ASSERT(m_ptr); return *this; } @@ -192,11 +198,29 @@ public: T* ptr() { return m_ptr; } const T* ptr() const { return m_ptr; } - T* operator->() { return m_ptr; } - const T* operator->() const { return m_ptr; } + T* operator->() + { + ASSERT(m_ptr); + return m_ptr; + } - T& operator*() { return *m_ptr; } - const T& operator*() const { return *m_ptr; } + const T* operator->() const + { + ASSERT(m_ptr); + return m_ptr; + } + + T& operator*() + { + ASSERT(m_ptr); + return *m_ptr; + } + + const T& operator*() const + { + ASSERT(m_ptr); + return *m_ptr; + } operator const T*() const { return m_ptr; } operator T*() { return m_ptr; }