From 04668106386c9b8ae9f62f56bef9378f2e1cb914 Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Fri, 12 Jun 2020 16:30:30 +0300 Subject: [PATCH] AK: Ensure we never use OwnPtr<> with RefCounted types --- AK/NonnullOwnPtr.h | 2 ++ AK/OwnPtr.h | 4 +++- AK/RefCounted.h | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/AK/NonnullOwnPtr.h b/AK/NonnullOwnPtr.h index 622290578a2..a9dd0cc474d 100644 --- a/AK/NonnullOwnPtr.h +++ b/AK/NonnullOwnPtr.h @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -51,6 +52,7 @@ public: NonnullOwnPtr(AdoptTag, T& ptr) : m_ptr(&ptr) { + static_assert(!is_ref_counted((const T*)nullptr), "Use RefPtr<> for RefCounted types"); } NonnullOwnPtr(NonnullOwnPtr&& other) : m_ptr(other.leak_ptr()) diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index ed351be0600..2ece450e81d 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -27,16 +27,18 @@ #pragma once #include +#include namespace AK { template class OwnPtr { public: - OwnPtr() {} + OwnPtr() { } explicit OwnPtr(T* ptr) : m_ptr(ptr) { + static_assert(!is_ref_counted((const T*)nullptr), "Use RefPtr<> for RefCounted types"); } OwnPtr(OwnPtr&& other) : m_ptr(other.leak_ptr()) diff --git a/AK/RefCounted.h b/AK/RefCounted.h index 361e6012861..98049405475 100644 --- a/AK/RefCounted.h +++ b/AK/RefCounted.h @@ -100,6 +100,16 @@ public: } }; +static constexpr bool is_ref_counted(const RefCountedBase*) +{ + return true; +} + +static constexpr bool is_ref_counted(...) +{ + return false; +} + } using AK::RefCounted;