Skip to content

Commit b408671

Browse files
Updated C6276
Matched formatting and wording with my other PRs. Added more information to the example
1 parent 8e66540 commit b408671

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

docs/code-quality/c6276.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ ms.assetid: 88f288da-da81-4d32-ab0f-be9d01a2606a
99
---
1010
# C6276
1111

12-
> 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
1317

1418
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.
1519

1620
## Example
1721

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):
1923

2024
```cpp
2125
#include <windows.h>
2226
VOID f()
2327
{
2428
WCHAR szBuffer[8];
2529
LPWSTR pSrc;
26-
2730
pSrc = (LPWSTR)"a";
2831
wcscpy(szBuffer, pSrc);
2932
}
@@ -38,13 +41,12 @@ VOID f()
3841
{
3942
WCHAR szBuffer[8];
4043
LPWSTR pSrc;
41-
4244
pSrc = L"a";
4345
wcscpy(szBuffer, pSrc);
4446
}
4547
```
4648

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:
4850

4951
```cpp
5052
#include <windows.h>

0 commit comments

Comments
 (0)