When should SecureZeroMemory() be used?

后端 未结 2 1725
深忆病人
深忆病人 2021-01-20 18:41

I found some sample code that looks like:

addrinfo hints;
SecureZeroMemory(&hints, sizeof(hints));

Is there a reason to use SecureZeroM

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 19:20

    From MSDN:

    Use this function instead of ZeroMemory when you want to ensure that your data will be overwritten promptly, as some C++ compilers can optimize a call to ZeroMemory by removing it entirely.

    Additionally a simple assignment, w/o a barrier, may be cached by hardware and not make it to the RAM for long time. Or a local variable may be optimized away. SecureZeroMemory makes sure none of that happens.

    As to why use for initializing addrinfo, a clearly non-security related concern, beats me.

提交回复
热议问题