Skip to content

Commit 36913a4

Browse files
authored
Merge pull request #4438 from MugBergerFries/patch-6
Updated C6276
2 parents dc0bd70 + ea50504 commit 36913a4

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

docs/code-quality/c6276.md

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,50 @@
11
---
22
description: "Learn more about: C6276"
33
title: C6276
4-
ms.date: 11/04/2016
4+
ms.date: 09/28/2022
55
ms.topic: reference
6-
f1_keywords: ["C6276"]
6+
f1_keywords: ["C6276", "CHAR_TO_WCHAR_CAST", "__WARNING_CHAR_TO_WCHAR_CAST"]
77
helpviewer_keywords: ["C6276"]
88
ms.assetid: 88f288da-da81-4d32-ab0f-be9d01a2606a
99
---
10-
# C6276
10+
# Warning 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+
> Cast between semantically different string types. Use of invalid string can lead to undefined behavior.
1313
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*`).
1515

16-
## Example
17-
18-
The following code generates this warning:
16+
## Remarks
1917

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
2621

27-
pSrc = (LPWSTR)"a";
28-
wcscpy(szBuffer, pSrc);
29-
}
30-
```
22+
## Example
3123

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

3426
```cpp
3527
#include <windows.h>
3628

37-
VOID f()
29+
void f()
3830
{
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);
4435
}
4536
```
4637

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

4940
```cpp
5041
#include <windows.h>
5142

52-
VOID f()
43+
void f()
5344
{
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);
5849
}
5950
```

0 commit comments

Comments
 (0)