mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
Add make_secure_unique helper
Co-authored-by: Pieter Wuille <bitcoin-dev@wuille.net>
This commit is contained in:
parent
c9f288244b
commit
d9841a7ac6
1 changed files with 24 additions and 0 deletions
|
@ -57,4 +57,28 @@ struct secure_allocator {
|
||||||
// TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well
|
// TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well
|
||||||
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
|
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct SecureUniqueDeleter {
|
||||||
|
void operator()(T* t) noexcept {
|
||||||
|
secure_allocator<T>().deallocate(t, 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using secure_unique_ptr = std::unique_ptr<T, SecureUniqueDeleter<T>>;
|
||||||
|
|
||||||
|
template<typename T, typename... Args>
|
||||||
|
secure_unique_ptr<T> make_secure_unique(Args&&... as)
|
||||||
|
{
|
||||||
|
T* p = secure_allocator<T>().allocate(1);
|
||||||
|
|
||||||
|
// initialize in place, and return as secure_unique_ptr
|
||||||
|
try {
|
||||||
|
return secure_unique_ptr<T>(new (p) T(std::forward(as)...));
|
||||||
|
} catch (...) {
|
||||||
|
secure_allocator<T>().deallocate(p, 1);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
|
#endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue