# V597. Compiler may delete 'memset' function call that is used to clear 'Foo' buffer. Use the RtlSecureZeroMemory() function to erase private data.

The analyzer has detected a potential error: an array containing private information is not cleared.

Consider the following code sample.

    void Foo()
    {
      char password[MAX_PASSWORD_LEN];
      InputPassword(password);
      ProcessPassword(password);
      memset(password, 0, sizeof(password));
    }

The function on the stack creates a temporary buffer intended for password storage. When we finish working with the password, we want to clear this buffer. If you don't do this, the password will remain in memory, which might lead to unpleasant consequences. Article about this: "Overwriting memory - why?".

Unfortunately, the code above may leave the buffer uncleared. Note that the 'password' array is cleared at the end and is not used anymore. That's why when building the Release version of the application, the compiler will most likely delete the call of the memset() function. The compiler has an absolute right to do that. This change does not affect the observed behavior which is described in the Standard as a sequence of calls of input-output functions and volatile data read-write functions. That is, from the viewpoint of the C/C++ language removing the call of the memset() function does not change anything!

To clear buffers containing private information you should use a special function RtlSecureZeroMemory() or memset_s() (see also "Safe Clearing of Private Data").

This is the fixed code:

    void Foo()
    {
      char password[MAX_PASSWORD_LEN];
      InputPassword(password);
      ProcessPassword(password);
      RtlSecureZeroMemory(password, sizeof(password));
    }

It seems that in practice the compiler cannot delete a call of such an important function as memset(). You might think that we speak of some exotic compilers. It's not so. Take the Visual C++ 10 compiler included into Visual Studio 2010, for instance.

Let's consider the two functions.

    void F1()
    {
      TCHAR buf[100];
      _stprintf(buf, _T("Test: %d"), 123);
      MessageBox(NULL, buf, NULL, MB_OK);
      memset(buf, 0, sizeof(buf));
    }

    void F2()
    {
      TCHAR buf[100];
      _stprintf(buf, _T("Test: %d"), 123);
      MessageBox(NULL, buf, NULL, MB_OK);
      RtlSecureZeroMemory(buf, sizeof(buf));
    }

The functions differ in the way they clear the buffer. The first one uses the memset() function, and the second the RtlSecureZeroMemory() function. Let's compile the optimized code enabling the "/O2" switch for the Visual C++ 10 compiler. Look at the assembler code we've got as a result:

Figure 1. The memset() function is removed.

Figure 2. The RtlSecureZeroMemory() function fills memory with nulls.

As you can see from the assembler code, the memset() function was deleted by the compiler during optimization, while the RtlSecureZeroMemory() function was arranged into the code, thus clearing the array successfully.

Additional materials on this topic:

- Safe Clearing of Private Data
- Security, security! But do you test it?
- Zero and forget - caveats of zeroing memory in C.

This diagnostic is classified as:

CWE-14 CERT-MSC06-C

You can look at examples of errors detected by the V597 diagnostic.