C6276

warning C6276: Cast between semantically different string types: char* to wchar_t*. Use of invalid string can lead to undefined behavior

This warning indicates a potentially incorrect cast from an ANSI string (char_t*) to a UNICODE string (wchar_t *). Because UNICODE strings have a character size of 2 bytes, this cast might yield strings that are not correctly terminated. Using such strings with the wcs* library of functions could cause buffer overruns and access violations.

Example

The following code generates this warning:

#include <windows.h>
VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;

   pSrc = (LPWSTR)"a";
   wcscpy(szBuffer, pSrc); 
}

The following code corrects this warning by appending the letter L to represent the ASCII character as a wide character:

#include <windows.h>

VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;

   pSrc = L"a";
   wcscpy(szBuffer, pSrc);
}

The following code uses the safe string manipulation function, wcscpy_s, to correct this warning:

#include <windows.h>

VOID f()
{
   WCHAR szBuffer[8];
   LPWSTR pSrc;
   pSrc = L"a";
   wcscpy_s(szBuffer,8,pSrc); 
}