• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 The Chromium 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 BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
6 #define BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
7 
8 #include <type_traits>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/memory/raw_ref.h"
12 #include "base/template_util.h"
13 
14 // It is dangerous to post a task with a T* argument where T is a subtype of
15 // RefCounted(Base|ThreadSafeBase), since by the time the parameter is used, the
16 // object may already have been deleted since it was not held with a
17 // scoped_refptr. Example: http://crbug.com/27191
18 // The following set of traits are designed to generate a compile error
19 // whenever this antipattern is attempted.
20 
21 namespace base::internal {
22 
23 template <typename T>
requires(T & x)24 concept IsRefCountedType = requires(T& x) {
25   // There are no additional constraints on `AddRef()` and `Release()` since
26   // `scoped_refptr`, for better or worse`, seamlessly interoperates with other
27   // non-base types that happen to implement the same signatures (e.g. COM's
28   // IUnknown).
29   x.AddRef();
30   x.Release();
31 };
32 
33 // Human readable translation: you needed to be a scoped_refptr if you are a raw
34 // pointer type and are convertible to a RefCounted(Base|ThreadSafeBase) type.
35 template <typename T>
36 struct NeedsScopedRefptrButGetsRawPtr {
37   static_assert(!std::is_reference_v<T>,
38                 "NeedsScopedRefptrButGetsRawPtr requires non-reference type.");
39 
40   // TODO(danakj): Should ban native references and
41   // std::reference_wrapper here too.
42   static constexpr bool value =
43       (base::IsRawRef<T>::value && IsRefCountedType<base::RemoveRawRefT<T>>) ||
44       (base::IsPointer<T>::value && IsRefCountedType<base::RemovePointerT<T>>);
45 };
46 
47 }  // namespace base::internal
48 
49 #endif  // BASE_MEMORY_RAW_SCOPED_REFPTR_MISMATCH_CHECKER_H_
50