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