Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
unowned_ptr.h
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CORE_FXCRT_UNOWNED_PTR_H_
6#define CORE_FXCRT_UNOWNED_PTR_H_
7
8// UnownedPtr is a smart pointer class that behaves very much like a
9// standard C-style pointer. The advantages of using it over native T*
10// pointers are:
11//
12// 1. It documents the nature of the pointer with no need to add a comment
13// explaining that is it // Not owned.
14//
15// 2. An attempt to delete an unowned ptr will fail to compile rather
16// than silently succeeding, since it is a class and not a raw pointer.
17//
18// 3. It is initialized to nullptr by default.
19//
20// When implemented via PartitionAlloc, additional properties apply.
21//
22// 4. When built using one of the dangling pointer detectors, the class
23// detects that the object being pointed to remains alive.
24//
25// 5. When built against PartitionAlloc's BRP feature, it provides the same
26// UaF protections as base::raw_ptr<T>
27//
28// Hence, when using UnownedPtr, no dangling pointers are ever permitted,
29// even if they are not de-referenced after becoming dangling. The style of
30// programming required is that the lifetime an object containing an
31// UnownedPtr must be strictly less than the object to which it points.
32//
33// The same checks are also performed at assignment time to prove that the
34// old value was not a dangling pointer.
35//
36// The array indexing operator[] is not supported on an unowned ptr,
37// because an unowned ptr expresses a one to one relationship with some
38// other heap object. Use pdfium::span<> for the cases where indexing
39// into an unowned array is desired, which performs the same checks.
40
41#include "build/build_config.h"
42#include "core/fxcrt/compiler_specific.h"
43
44#if defined(PDF_USE_PARTITION_ALLOC)
45#include "partition_alloc/partition_alloc_buildflags.h"
46#include "partition_alloc/pointers/raw_ptr.h"
47
48#if !PA_BUILDFLAG(USE_PARTITION_ALLOC)
49#error "pdf_use_partition_alloc=true requires use_partition_alloc=true"
50#endif
51
52#if PA_BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS) ||
53 PA_BUILDFLAG(USE_RAW_PTR_ASAN_UNOWNED_IMPL)
54#define UNOWNED_PTR_DANGLING_CHECKS
55#endif
56
57static_assert(raw_ptr<int>::kZeroOnConstruct, "Unsafe build arguments");
58static_assert(raw_ptr<int>::kZeroOnMove, "Unsafe build arguments");
59
60template <typename T>
61using UnownedPtr = raw_ptr<T>;
62
63#else // defined(PDF_USE_PARTITION_ALLOC)
64
65#include <cstddef>
66#include <cstdint>
67#include <functional>
68#include <type_traits>
69#include <utility>
70
71#include "core/fxcrt/unowned_ptr_exclusion.h"
72
73namespace fxcrt {
74
75template <class T>
77 public:
78 constexpr UnownedPtr() noexcept = default;
79
80 // Deliberately implicit to allow returning nullptrs.
81 // NOLINTNEXTLINE(runtime/explicit)
82 constexpr UnownedPtr(std::nullptr_t ptr) {}
83
84 explicit constexpr UnownedPtr(T* pObj) noexcept : m_pObj(pObj) {}
85
86 // Copy-construct an UnownedPtr.
87 // Required in addition to copy conversion constructor below.
88 constexpr UnownedPtr(const UnownedPtr& that) noexcept = default;
89
90 // Move-construct an UnownedPtr. After construction, |that| will be NULL.
91 // Required in addition to move conversion constructor below.
92 constexpr UnownedPtr(UnownedPtr&& that) noexcept
93 : m_pObj(that.ExtractAsDangling()) {}
94
95 // Copy-conversion constructor.
96 template <class U,
97 typename = typename std::enable_if<
99 UnownedPtr(const UnownedPtr<U>& that) : m_pObj(static_cast<U*>(that)) {}
100
101 // Move-conversion constructor.
102 template <class U,
103 typename = typename std::enable_if<
104 std::is_convertible<U*, T*>::value>::type>
105 UnownedPtr(UnownedPtr<U>&& that) noexcept
106 : m_pObj(that.ExtractAsDangling()) {}
107
108 // Assign an UnownedPtr from nullptr.
109 UnownedPtr& operator=(std::nullptr_t) noexcept {
110 m_pObj = nullptr;
111 return *this;
112 }
113
114 // Assign an UnownedPtr from a raw ptr.
115 UnownedPtr& operator=(T* that) noexcept {
116 m_pObj = that;
117 return *this;
118 }
119
120 // Copy-assign an UnownedPtr.
121 // Required in addition to copy conversion assignment below.
122 UnownedPtr& operator=(const UnownedPtr& that) noexcept = default;
123
124 // Move-assign an UnownedPtr. After assignment, |that| will be NULL.
125 // Required in addition to move conversion assignment below.
126 UnownedPtr& operator=(UnownedPtr&& that) noexcept {
127 if (*this != that) {
128 m_pObj = that.ExtractAsDangling();
129 }
130 return *this;
131 }
132
133 // Copy-convert assignment.
134 template <class U,
135 typename = typename std::enable_if<
136 std::is_convertible<U*, T*>::value>::type>
137 UnownedPtr& operator=(const UnownedPtr<U>& that) noexcept {
138 if (*this != that) {
139 m_pObj = static_cast<U*>(that);
140 }
141 return *this;
142 }
143
144 // Move-convert assignment. After assignment, |that| will be NULL.
145 template <class U,
146 typename = typename std::enable_if<
147 std::is_convertible<U*, T*>::value>::type>
148 UnownedPtr& operator=(UnownedPtr<U>&& that) noexcept {
149 if (*this != that) {
150 m_pObj = that.ExtractAsDangling();
151 }
152 return *this;
153 }
154
156 m_pObj = nullptr;
157 }
158
159 bool operator==(std::nullptr_t ptr) const { return m_pObj == nullptr; }
160 bool operator==(const UnownedPtr& that) const {
161 return m_pObj == static_cast<T*>(that);
162 }
163 bool operator<(const UnownedPtr& that) const {
164 return std::less<T*>()(m_pObj, static_cast<T*>(that));
165 }
166
167 operator T*() const noexcept { return m_pObj; }
168 T* get() const noexcept { return m_pObj; }
169
170 T* ExtractAsDangling() { return std::exchange(m_pObj, nullptr); }
171 void ClearAndDelete() { delete std::exchange(m_pObj, nullptr); }
172
173 explicit operator bool() const { return !!m_pObj; }
174 T& operator*() const { return *m_pObj; }
175 T* operator->() const { return m_pObj; }
176
177 private:
178 UNOWNED_PTR_EXCLUSION T* m_pObj = nullptr;
179};
180
181} // namespace fxcrt
182
183using fxcrt::UnownedPtr;
184
185#endif // defined(PDF_USE_PARTITION_ALLOC)
186
187namespace pdfium {
188
189// Type-deducing wrapper to make an UnownedPtr from an ordinary pointer,
190// since equivalent constructor is explicit.
191template <typename T>
193 return UnownedPtr<T>(that);
194}
195
196} // namespace pdfium
197
198#endif // CORE_FXCRT_UNOWNED_PTR_H_
fxcrt::ByteString ByteString
Definition bytestring.h:180
#define DCHECK
Definition check.h:33
std::vector< RetainPtr< CPDF_Object > >::const_iterator const_iterator
Definition cpdf_array.h:29
CPDF_Creator(CPDF_Document *pDoc, RetainPtr< IFX_RetainableWriteStream > archive)
bool SetFileVersion(int32_t fileVersion)
void RemoveSecurity()
bool Create(uint32_t flags)
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
virtual FX_FILESIZE CurrentOffset() const =0
static RetainPtr< IFX_SeekableReadStream > CreateFromFilename(const char *filename)
Definition fx_stream.cpp:68
virtual FX_FILESIZE GetPosition()
Definition fx_stream.cpp:80
virtual bool ReadBlockAtOffset(pdfium::span< uint8_t > buffer, FX_FILESIZE offset)=0
virtual bool IsEOF()
Definition fx_stream.cpp:76
virtual bool Flush()=0
virtual FX_FILESIZE GetSize()=0
bool WriteFilesize(FX_FILESIZE size)
Definition fx_stream.cpp:61
bool WriteByte(uint8_t byte)
Definition fx_stream.cpp:50
bool WriteString(ByteStringView str)
Definition fx_stream.cpp:46
virtual ~IFX_WriteStream()=default
bool WriteDWord(uint32_t i)
Definition fx_stream.cpp:54
virtual bool WriteBlock(pdfium::span< const uint8_t > data)=0
static ByteString Format(const char *pFormat,...)
ByteString & operator=(ByteString &&that) noexcept
T * get() const noexcept
UnownedPtr(const UnownedPtr< U > &that)
Definition unowned_ptr.h:99
bool operator==(const UnownedPtr &that) const
operator T*() const noexcept
UnownedPtr(UnownedPtr< U > &&that) noexcept
UnownedPtr & operator=(UnownedPtr< U > &&that) noexcept
constexpr UnownedPtr(const UnownedPtr &that) noexcept=default
constexpr UnownedPtr(T *pObj) noexcept
Definition unowned_ptr.h:84
UnownedPtr & operator=(T *that) noexcept
bool operator==(std::nullptr_t ptr) const
UnownedPtr & operator=(UnownedPtr &&that) noexcept
UnownedPtr & operator=(std::nullptr_t) noexcept
UnownedPtr & operator=(const UnownedPtr< U > &that) noexcept
T * operator->() const
constexpr UnownedPtr() noexcept=default
bool operator<(const UnownedPtr &that) const
UnownedPtr & operator=(const UnownedPtr &that) noexcept=default
T & operator*() const
constexpr UnownedPtr(UnownedPtr &&that) noexcept
Definition unowned_ptr.h:92
constexpr UnownedPtr(std::nullptr_t ptr)
Definition unowned_ptr.h:82
#define GSL_POINTER
#define TRIVIAL_ABI
#define FPDFCREATE_INCREMENTAL
#define FPDFCREATE_NO_ORIGINAL
void * FX_Random_MT_Start(uint32_t dwSeed)
Definition fx_random.cpp:87
void FX_Random_MT_Close(void *pContext)
pdfium::CheckedNumeric< FX_FILESIZE > FX_SAFE_FILESIZE
#define FX_FILESIZE
Definition fx_types.h:19
UnownedPtr< T > WrapUnowned(T *that)
fxcrt::ByteStringView ByteStringView
#define UNOWNED_PTR_EXCLUSION