From: Vsevolod Stakhov Date: Mon, 6 Jun 2022 20:42:09 +0000 (+0100) Subject: [Minor] Add a memory erasing allocator X-Git-Tag: 3.3~209 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=adeb78949c10dd6bedff71cc5530e9505a7a4c63;p=thirdparty%2Frspamd.git [Minor] Add a memory erasing allocator --- diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx index 0df3349a3d..2e0308bd20 100644 --- a/src/libutil/cxx/util.hxx +++ b/src/libutil/cxx/util.hxx @@ -129,6 +129,27 @@ constexpr auto enumerate(T && iterable) return iterable_wrapper{ std::forward(iterable) }; } +/** + * Allocator that cleans up memory in a secure way on destruction + * @tparam T + */ +template class secure_mem_allocator : public std::allocator +{ +public: + using pointer = typename std::allocator::pointer; + using size_type = typename std::allocator::size_type; + template struct rebind { typedef secure_mem_allocator other; }; + secure_mem_allocator() noexcept = default; + secure_mem_allocator(const secure_mem_allocator &) noexcept {} + template explicit secure_mem_allocator(const secure_mem_allocator&) noexcept {} + + void deallocate(pointer p, size_type num) noexcept { + rspamd_explicit_memzero((void *)p, num); + std::allocator::deallocate(p, num); + } +}; + + } #endif //RSPAMD_UTIL_HXX