gpt4 book ai didi

c++ - Linux (gcc 5.4) 中是否有任何特定于 Windows 的 SecureZeroMemory 等效库函数可用?

转载 作者:太空宇宙 更新时间:2023-11-04 11:57:51 26 4
gpt4 key购买 nike

我们编写了可在 Windows 和 Ubuntu 上编译的 C++ 代码。我想使用安全函数 memset_s 将我的应用程序中的缓冲区清零。

Windows 和 Ubuntu 16.04 都不包含 __STDC_LIB_EXT1__ 宏的定义,所以我无法使用 C++11 提供的 memset_s

在 Windows 上,我有 SecureZeroMemory 可以防止编译器优化。在 Ubuntu 16.04(GCC 5.4 版)上,我正在寻找 SecureZeroMemory 等效库函数(这将阻止编译器优化)。

如果你能建议我在 linux 中的库函数,这将非常有帮助,结束选项是 linux 平台的手动实现 memset_s

提前致谢

最佳答案

如果你想确保内存归零并且这个操作没有被编译器优化(因为 as-if 规则),你可以放置一个“优化器屏障”:

void always_memset(void* dest,int ch,size_t count){
memset(dest,ch,count);
//Make the compiler believe that you are using memory
//including memory referenced by dest
asm volatile("" : : :"memory");//it is possible to be more subtle.

}

然后 memset_s 可以很容易地通过在先决条件检查之后调用这个函数来实现。

关于c++ - Linux (gcc 5.4) 中是否有任何特定于 Windows 的 SecureZeroMemory 等效库函数可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53606684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com