Bitcoin Core  22.99.0
P2P Digital Currency
zeroafterfree.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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  using base = std::allocator<T>;
17  using traits = std::allocator_traits<base>;
18  using size_type = typename traits::size_type;
19  using difference_type = typename traits::difference_type;
20  using pointer = typename traits::pointer;
21  using const_pointer = typename traits::const_pointer;
22  using value_type = typename traits::value_type;
25  template <typename U>
27  {
28  }
30  template <typename _Other>
31  struct rebind {
33  };
34 
35  void deallocate(T* p, std::size_t n)
36  {
37  if (p != nullptr)
38  memory_cleanse(p, sizeof(T) * n);
39  std::allocator<T>::deallocate(p, n);
40  }
41 };
42 
44 using SerializeData = std::vector<uint8_t, zero_after_free_allocator<uint8_t>>;
45 
46 #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
zero_after_free_allocator::difference_type
typename traits::difference_type difference_type
Definition: zeroafterfree.h:19
SerializeData
std::vector< uint8_t, zero_after_free_allocator< uint8_t > > SerializeData
Byte-vector that clears its contents before deletion.
Definition: zeroafterfree.h:44
zero_after_free_allocator::~zero_after_free_allocator
~zero_after_free_allocator() noexcept
Definition: zeroafterfree.h:29
memory_cleanse
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::traits
std::allocator_traits< base > traits
Definition: zeroafterfree.h:17
zero_after_free_allocator::pointer
typename traits::pointer pointer
Definition: zeroafterfree.h:20
zero_after_free_allocator::value_type
typename traits::value_type value_type
Definition: zeroafterfree.h:22
zero_after_free_allocator::rebind
Definition: zeroafterfree.h:31
zero_after_free_allocator
Definition: zeroafterfree.h:15
cleanse.h
zero_after_free_allocator::base
std::allocator< T > base
Definition: zeroafterfree.h:16
zero_after_free_allocator::zero_after_free_allocator
zero_after_free_allocator() noexcept
Definition: zeroafterfree.h:23
zero_after_free_allocator::zero_after_free_allocator
zero_after_free_allocator(const zero_after_free_allocator< U > &a) noexcept
Definition: zeroafterfree.h:26
ByteUnit::T
@ T
zero_after_free_allocator::deallocate
void deallocate(T *p, std::size_t n)
Definition: zeroafterfree.h:35
zero_after_free_allocator::rebind::other
zero_after_free_allocator< _Other > other
Definition: zeroafterfree.h:32
zero_after_free_allocator::size_type
typename traits::size_type size_type
Definition: zeroafterfree.h:18
zero_after_free_allocator::zero_after_free_allocator
zero_after_free_allocator(const zero_after_free_allocator &a) noexcept
Definition: zeroafterfree.h:24
zero_after_free_allocator::const_pointer
typename traits::const_pointer const_pointer
Definition: zeroafterfree.h:21