I am using Delphi 7 IDE. Does Delphi compiler optimize codes, just like what the C++ compiler is doing in this following link?
http://msdn.microsoft.com/en-us/library/aa366877(VS.85).aspx
WCHAR szPassword[MAX_PATH];
// Retrieve the password
if (GetPasswordFromUser(szPassword, MAX_PATH))
UsePassword(szPassword);
// Clear the password from memory
SecureZeroMemory(szPassword, sizeof(szPassword));
If ZeroMemory
were called in this example instead of SecureZeroMemory
, the compiler could optimize the call because the szPassword
buffer is not read from before it goes out of scope. The password would remain on the application stack where it could be captured in a crash dump or probed by a malicious application.
Yes, of course Delphi performs optimizations. However, it does not perform the optimization that the SecureZeroMemory
function is meant to circumvent. There is no need to use that function in Delphi; just use plain old ZeroMemory
, or even FillChar
. They're not macros, and they don't do anything that Delphi recognizes as being unused assignment statements that could get optimized out.