【发布时间】:2010-09-24 08:23:38
【问题描述】:
我必须编写一个 Windows 服务,在某些时候处理机密数据(例如 PIN 码、密码等)。这些信息需要很短的时间:通常它们几乎立即发送到智能卡读卡器。
让我们考虑这段代码:
{
std::string password = getPassword(); // Get the password from the user
writePasswordToSmartCard(password);
// Okay, here we don't need password anymore.
// We set it all to '\0' so it doesn't stay in memory.
std::fill(password.begin(), password.end(), '\0');
}
现在我关心的是编译器优化。这里编译器可能会检测到密码即将被删除,此时更改其值是无用的,只需删除调用即可。
我不希望我的编译器关心未来未引用内存的值。
我的担忧合理吗?我如何确定这样一段代码不会被优化出来?
【问题讨论】:
标签: c++ security optimization memory compiler-construction