So I'm really intrigued about whether or not it can survive aggressive optimization tactics employed by GCC and clang.
Considering the following example:
void* clean(void* pointer, std::size_t size) noexcept
{
return new(pointer) char[size]{};
}
void doStuff()
{
//...
clean(pointer, size);
//...
}
Can I trust it with the task of cleaning sensitive data?
I do not think optimization can play any tricks on you here. Standard mandates value initialization in this case: new(pointer) char[size]{}
, so after this call memory pointed to by pointer
would be filled with 0.
May be compiler can optimize it if you never access the new pointer or override it before accessin (based on observability). If you want to avoid this slight possibility, you'd need to define your pointer as a pointer to volatile
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With