Bitcoin ABC  0.22.12
P2P Digital Currency
zeroafterfree.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
7 #define BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
8 
9 #include <support/cleanse.h>
10 
11 #include <memory>
12 #include <vector>
13 
14 template <typename T>
15 struct zero_after_free_allocator : public std::allocator<T> {
16  // MSVC8 default copy constructor is broken
17  typedef std::allocator<T> base;
18  typedef typename base::size_type size_type;
19  typedef typename base::difference_type difference_type;
20  typedef typename base::pointer pointer;
21  typedef typename base::const_pointer const_pointer;
22  typedef typename base::reference reference;
23  typedef typename base::const_reference const_reference;
24  typedef typename base::value_type value_type;
27  : base(a) {}
28  template <typename U>
30  : base(a) {}
32  template <typename _Other> struct rebind {
34  };
35 
36  void deallocate(T *p, std::size_t n) {
37  if (p != nullptr) memory_cleanse(p, sizeof(T) * n);
38  std::allocator<T>::deallocate(p, n);
39  }
40 };
41 
42 // Byte-vector that clears its contents before deletion.
43 typedef std::vector<char, zero_after_free_allocator<char>> CSerializeData;
44 
45 #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
base::const_pointer const_pointer
Definition: zeroafterfree.h:21
~zero_after_free_allocator() noexcept
Definition: zeroafterfree.h:31
base::value_type value_type
Definition: zeroafterfree.h:24
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
zero_after_free_allocator< _Other > other
Definition: zeroafterfree.h:33
base::const_reference const_reference
Definition: zeroafterfree.h:23
zero_after_free_allocator() noexcept
Definition: zeroafterfree.h:25
base::size_type size_type
Definition: zeroafterfree.h:18
zero_after_free_allocator(const zero_after_free_allocator< U > &a) noexcept
Definition: zeroafterfree.h:29
std::allocator< T > base
Definition: zeroafterfree.h:17
void deallocate(T *p, std::size_t n)
Definition: zeroafterfree.h:36
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: zeroafterfree.h:43
base::difference_type difference_type
Definition: zeroafterfree.h:19
zero_after_free_allocator(const zero_after_free_allocator &a) noexcept
Definition: zeroafterfree.h:26
base::reference reference
Definition: zeroafterfree.h:22