Skip to content

Commit d9841a7

Browse files
ajtownsPieter Wuille
authored andcommitted
Add make_secure_unique helper
Co-authored-by: Pieter Wuille <[email protected]>
1 parent c9f2882 commit d9841a7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/support/allocators/secure.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,28 @@ struct secure_allocator {
5757
// TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well
5858
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
5959

60+
template<typename T>
61+
struct SecureUniqueDeleter {
62+
void operator()(T* t) noexcept {
63+
secure_allocator<T>().deallocate(t, 1);
64+
}
65+
};
66+
67+
template<typename T>
68+
using secure_unique_ptr = std::unique_ptr<T, SecureUniqueDeleter<T>>;
69+
70+
template<typename T, typename... Args>
71+
secure_unique_ptr<T> make_secure_unique(Args&&... as)
72+
{
73+
T* p = secure_allocator<T>().allocate(1);
74+
75+
// initialize in place, and return as secure_unique_ptr
76+
try {
77+
return secure_unique_ptr<T>(new (p) T(std::forward(as)...));
78+
} catch (...) {
79+
secure_allocator<T>().deallocate(p, 1);
80+
throw;
81+
}
82+
}
83+
6084
#endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H

0 commit comments

Comments
 (0)