LCOV - code coverage report
Current view: top level - src/support/allocators - zeroafterfree.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 7 7 100.0 %
Date: 2021-06-20 09:29:20 Functions: 3 3 100.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       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                 :            :     // 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;
      25                 :    2917702 :     zero_after_free_allocator() noexcept {}
      26                 :    6990076 :     zero_after_free_allocator(const zero_after_free_allocator& a) noexcept : base(a) {}
      27                 :            :     template <typename U>
      28                 :            :     zero_after_free_allocator(const zero_after_free_allocator<U>& a) noexcept : base(a)
      29                 :            :     {
      30                 :            :     }
      31                 :            :     ~zero_after_free_allocator() noexcept {}
      32                 :            :     template <typename _Other>
      33                 :            :     struct rebind {
      34                 :            :         typedef zero_after_free_allocator<_Other> other;
      35                 :            :     };
      36                 :            : 
      37                 :    4425167 :     void deallocate(T* p, std::size_t n)
      38                 :            :     {
      39         [ +  - ]:    4425167 :         if (p != nullptr)
      40                 :    4425167 :             memory_cleanse(p, sizeof(T) * n);
      41                 :    4425167 :         std::allocator<T>::deallocate(p, n);
      42                 :    4425167 :     }
      43                 :            : };
      44                 :            : 
      45                 :            : /** Byte-vector that clears its contents before deletion. */
      46                 :            : using SerializeData = std::vector<uint8_t, zero_after_free_allocator<uint8_t>>;
      47                 :            : 
      48                 :            : #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H

Generated by: LCOV version 1.14