Edit

Share via


Warning C6054

String 'variable' may not be zero-terminated.

Remarks

This warning indicates that a function that requires a zero-terminated string was passed a non-zero terminated string. A function that expects a zero-terminated string could look for the zero beyond the end of the buffer. This defect might cause an exploitable buffer overrun error or crash. The program should make sure the string passed in ends with a zero.

Code analysis name: MISSING_ZERO_TERMINATION2

Example

The following code generates this warning:

// Warning C6054_bad.cpp
// Compile using: cl /W4 /EHsc /c /analyze C6054_bad.cpp
#include <sal.h>

void func( _In_z_ wchar_t* wszStr );

void g ( )
{
    wchar_t wcArray[200] = { 'h', 'e', 'l', 'l', 'o' };
    func(wcArray); // Warning C6054
}

To correct this warning, null-terminate wcArray before calling function func() as shown in the following sample code:

// C6054_good.cpp
// Compile using: cl /W4 /EHsc /c /analyze C6054_good.cpp
#include <sal.h>

void func( _In_z_ wchar_t* wszStr );

void g ( )
{
    wchar_t wcArray[200] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    func(wcArray); // OK
}

See also