Skip to content

Commit fc6ce60

Browse files
Updated C6276
Removed third example and corrected earlier examples. Changed 3 space indentation to 4 to be consistent with my other PRs. Fixed spacing
1 parent 9e5cdb4 commit fc6ce60

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

docs/code-quality/c6276.md

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,24 @@ The following code generates this warning. This issue stems from the improper ca
2525

2626
```cpp
2727
#include <windows.h>
28-
VOID f()
28+
void f()
2929
{
30-
WCHAR szBuffer[8];
31-
LPWSTR pSrc;
32-
pSrc = (LPWSTR)"a";
33-
wcscpy(szBuffer, pSrc);
30+
WCHAR szBuffer[8];
31+
LPWSTR pSrc;
32+
pSrc = (LPWSTR)"a";
33+
wcscpy_s(szBuffer, pSrc);
3434
}
3535
```
3636

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

3939
```cpp
4040
#include <windows.h>
41-
42-
VOID f()
43-
{
44-
WCHAR szBuffer[8];
45-
LPWSTR pSrc;
46-
pSrc = L"a";
47-
wcscpy(szBuffer, pSrc);
48-
}
49-
```
50-
51-
For an extra layer of security, the following code uses the safe string manipulation function `wcscpy_s` to correct this warning. This change limits the copy to 8 bytes, the size of the buffer being copied to:
52-
53-
```cpp
54-
#include <windows.h>
55-
56-
VOID f()
41+
void f()
5742
{
58-
WCHAR szBuffer[8];
59-
LPWSTR pSrc;
60-
pSrc = L"a";
61-
wcscpy_s(szBuffer,8,pSrc);
43+
WCHAR szBuffer[8];
44+
LPWSTR pSrc;
45+
pSrc = L"a";
46+
wcscpy_s(szBuffer, pSrc);
6247
}
6348
```

0 commit comments

Comments
 (0)