You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> warning C6276: Cast between semantically different string types: char* to wchar_t\*. Use of invalid string can lead to undefined behavior
12
+
**Warning C2676: CHAR to WCHAR cast(CHAR_TO_WCHAR_CAST)**\
13
+
Example output:
14
+
> Cast between semantically different string types: char* to wchar_t\*. Use of invalid string can lead to undefined behavior.
15
+
16
+
## Description
13
17
14
18
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.
15
19
16
20
## Example
17
21
18
-
The following code generates this warning:
22
+
The following code generates this warning. This issue stems from the improper casting of the character 'a' (1 byte) to a Unicode character (2 bytes):
19
23
20
24
```cpp
21
25
#include<windows.h>
22
26
VOID f()
23
27
{
24
28
WCHAR szBuffer[8];
25
29
LPWSTR pSrc;
26
-
27
30
pSrc = (LPWSTR)"a";
28
31
wcscpy(szBuffer, pSrc);
29
32
}
@@ -38,13 +41,12 @@ VOID f()
38
41
{
39
42
WCHAR szBuffer[8];
40
43
LPWSTR pSrc;
41
-
42
44
pSrc = L"a";
43
45
wcscpy(szBuffer, pSrc);
44
46
}
45
47
```
46
48
47
-
The following code uses the safe string manipulation function,`wcscpy_s`, to correct this warning:
49
+
For an additional layer of security, the following code uses the safe string manipulation function `wcscpy_s` to correct this warning. This limits the copy to 8 bytes, the size of the buffer being copied to:
0 commit comments