|
1 | 1 | ---
|
2 | 2 | description: "Learn more about: C6276"
|
3 | 3 | title: C6276
|
4 |
| -ms.date: 11/04/2016 |
| 4 | +ms.date: 09/28/2022 |
5 | 5 | ms.topic: reference
|
6 |
| -f1_keywords: ["C6276"] |
| 6 | +f1_keywords: ["C6276", "CHAR_TO_WCHAR_CAST", "__WARNING_CHAR_TO_WCHAR_CAST"] |
7 | 7 | helpviewer_keywords: ["C6276"]
|
8 | 8 | ms.assetid: 88f288da-da81-4d32-ab0f-be9d01a2606a
|
9 | 9 | ---
|
10 |
| -# C6276 |
| 10 | +# Warning C6276 |
11 | 11 |
|
12 |
| -> warning C6276: Cast between semantically different string types: char* to wchar_t\*. Use of invalid string can lead to undefined behavior |
| 12 | +> Cast between semantically different string types. Use of invalid string can lead to undefined behavior. |
13 | 13 |
|
14 |
| -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. |
| 14 | +This warning indicates a potentially incorrect cast from a narrow character string (`char*`) to a wide character string (`wchar_t*`). |
15 | 15 |
|
16 |
| -## Example |
17 |
| - |
18 |
| -The following code generates this warning: |
| 16 | +## Remarks |
19 | 17 |
|
20 |
| -```cpp |
21 |
| -#include <windows.h> |
22 |
| -VOID f() |
23 |
| -{ |
24 |
| - WCHAR szBuffer[8]; |
25 |
| - LPWSTR pSrc; |
| 18 | +Because the Microsoft compiler implements wide strings with a character size of 2 bytes, casting from a narrow string might produce strings that aren't correctly terminated. If you use such strings with the `wcs*` functions in the runtime library, they could cause buffer overruns and access violations. |
| 19 | + |
| 20 | +Code analysis name: CHAR_TO_WCHAR_CAST |
26 | 21 |
|
27 |
| - pSrc = (LPWSTR)"a"; |
28 |
| - wcscpy(szBuffer, pSrc); |
29 |
| -} |
30 |
| -``` |
| 22 | +## Example |
31 | 23 |
|
32 |
| -The following code corrects this warning by appending the letter L to represent the ASCII character as a wide character: |
| 24 | +The following code generates warning C6276. It's caused by an improper cast of the narrow string "a" (2 bytes, one for the 'a' and one for the null terminator) to a wide string (a 2-byte wide character 'a' with no null terminator): |
33 | 25 |
|
34 | 26 | ```cpp
|
35 | 27 | #include <windows.h>
|
36 | 28 |
|
37 |
| -VOID f() |
| 29 | +void f() |
38 | 30 | {
|
39 |
| - WCHAR szBuffer[8]; |
40 |
| - LPWSTR pSrc; |
41 |
| - |
42 |
| - pSrc = L"a"; |
43 |
| - wcscpy(szBuffer, pSrc); |
| 31 | + WCHAR szBuffer[8]; |
| 32 | + LPWSTR pSrc; |
| 33 | + pSrc = (LPWSTR)"a"; |
| 34 | + wcscpy_s(szBuffer, pSrc); |
44 | 35 | }
|
45 | 36 | ```
|
46 | 37 |
|
47 |
| -The following code uses the safe string manipulation function, `wcscpy_s`, to correct this warning: |
| 38 | +The following code corrects this warning. It removes the problem cast and adds an `L` prefix to the string to define it as a properly terminated wide character string: |
48 | 39 |
|
49 | 40 | ```cpp
|
50 | 41 | #include <windows.h>
|
51 | 42 |
|
52 |
| -VOID f() |
| 43 | +void f() |
53 | 44 | {
|
54 |
| - WCHAR szBuffer[8]; |
55 |
| - LPWSTR pSrc; |
56 |
| - pSrc = L"a"; |
57 |
| - wcscpy_s(szBuffer,8,pSrc); |
| 45 | + WCHAR szBuffer[8]; |
| 46 | + LPWSTR pSrc; |
| 47 | + pSrc = L"a"; |
| 48 | + wcscpy_s(szBuffer, pSrc); |
58 | 49 | }
|
59 | 50 | ```
|
0 commit comments