Skip to content

Commit 4e0c557

Browse files
TaojunshenMugBergerFriesMichaelSquiresbradlitterellopbld17
authored
9/29/2022 AM Publish (#4551)
* Updated C6101 Mirror of my public PR. Added an example. Added proper spacing between headers and code blocks, matched formatting to my other PRs * Updated C6101 Minor changes in response to Acrolinx report * Updated C6101 More Acrolinx changes, split a run-on sentence * Updated C26495 Matched formatting to my other PRs. Added more information to the example. * Updated C26495 Fixed spelling error * Updated C6200 Matched formatting and wording to my other PRs. Added more information to the example * Updated C6200 Reworded to appease high-lord Acrolinx * Updated C6201 Matched formatting and wording to my other PRs. Matched the example to C6200 since they were functionally the same. * Updated C6200 Fixed output * Updated C6276 Matched formatting and wording with my other PRs. Added more information to the example * Updated C6276 Small to changes at the behest of Acrolinx * Updated C6277 Changed wording slightly, matched formatting and wording to my other PRs * Updated C6277 Changed wording, acquiesced to Acrolinx's request * Updated C6308 Changed wording to be more concise and less hostile. Changed the format and wording to match my other PRs * Updated C6308 Acrolinx changes * Updated C6200 Updated to the new format * Updated C6201 Updated to the new format * Updated C6201 Added code analysis name * Updated C6200 Added code analysis name * Updated C6276 Updated to the new format * Updated C6277 Updated to the new format * Update docs/code-quality/c6200.md Co-authored-by: Michael Squires <[email protected]> * Update docs/code-quality/c6200.md Co-authored-by: Michael Squires <[email protected]> * Updated C6200 Spacing fixed * Update docs/code-quality/c6201.md Co-authored-by: Michael Squires <[email protected]> * Updated C6201 Spacing fixed * Updated C6276 Removed third example and corrected earlier examples. Changed 3 space indentation to 4 to be consistent with my other PRs. Fixed spacing * Updated C6276 Consistent spacing * Updated C6101 Updated to the latest format * Updated C6101 Single backticks and InOut->Inout * Updated C6308 Updated to the new format, spacing made consistent with my other PRs * Updated C26495 Updated to the new format NOTE: This warning has no code analysis name. Per @MichaelSquires instructions, I omitted the usual keywords and 'Code analysis name:' section to match * Update compiler-warning-level-4-c4464.md @corob-msft proposal. I don't warrant it shouldn't be worded in a better way. * Update compiler-warning-level-4-c4464.md typo fix * Updated C6276 UNICODE/ANSI -> wide/narrow * Updated C6276 an -> a * Updated C6276 Updated to display new warning message * Updated C6276 char_t -> char * Updated C6201 Example now correctly shows a stack array * Updated C6200 Made example more obvious that the array was heap-allocated * Updated C6200 Added brackets to delete * Update docs/code-quality/c6308.md Co-authored-by: Michael Squires <[email protected]> * fix: delete unnecessary asterisk followup fix of MicrosoftDocs#4136 * optimize prime test * Update floating-point-support.md Explain what "ulp" stands for * Updated C6308 Changed example to initialize and declare variable sin the same line * Updates for cpp-docs 4161 * Update integritycheck-require-signature-check.md * alt-text * Update abstract-cpp-component-extensions.md * Add syntax highlighting to atl-mfc-shared * Updated C26495 Added code analysis ID * Updated C6201 Added stack-specific consequence to the description * alt-text updates * customer fix * minor updates * Incorporate changes in rewrite Updates for style and clarity. Also give it an Acrolinx pass. * Clarify version support * Tweak language for minimum support * Add code style, comment @MugBergerFries It's a good idea to use code styling for mentions of variable or class names (especially when they're easily confused generic names like "value"). I also added a comment to the fixed code sample to amplify the change and what it was doing. * acrolinx * cleanup pass * [BULK] DocuTune - Rebranding * alt-text updates * update alt-text * Fix Supported SSH algorithms * Update README.md * Address cpp-docs 4149 __umulh on ARM64 * Add slashes per cpp-docs 4150 * update alt-text code and removed 'experimental' from images * Update clang-support-msbuild.md Updated for better visibility of LLVMToolsVersion property. Note the text was also misleading because it kind of implies that LLVMToolsVersion supports side-by-side version of clang/LLVM in the same `LLVMInstallDir`, but that's not how clang/LLVM works. If you want to select a different version, you need to point it to a different path. * test fix * test fix * test fix * alt-text updates plus acrolinx * acrolinx * Split props file and IDE sections When both path and version are merged in the .props file discussion, a division between the .props approach and the IDE approach seems more natural than repeating the same content twice. * Update to add /std requirement * Test of build checks features * Update cpp-linter-overview.md * Provide F1 links to articles for VS linker properties (#4529) * Update F1 links for Linker Properties * F1 Project: Linker Properties part 1 * Finish Linker properties F1 links * Fix typo * Fix acrolinx related issues * Update dot-xmm.md Fixed overzealous replacement * [BULK UPDATE] DocuTune - Rebranding * Fix missing semicolon * Update files per cpp-docs 4181 * Add /ILK option per cpp-docs 4194 * Update per cpp-docs 4193 * Address cpp-docs 4186 code labels * template function -> function template * Acrolinx fixes. * Tweak language for 4178 update * Updates to main and wmain signatures * Acrolinx and style updates per review * Fix spelling, i18n, style, Acrolinx issues * Fix date. * Updated C6200 Removed extra asterisk in warning output * Updated C6201 Removed extra asterisks * Updated C6277 Removed extra asterisk * Updated C6308 Removed extra asterisk * Use correct warning message for C6201 Incorrectly copied warning message from C6200. * Quote actual warning message Quote the actual message, bad grammar and all, to make search happy. * Remove undefined quotes Simplify. Update date. Remove quotes from fully qualified, since we don't define it. * Give library functions code style Fix date. Also tweak a sentence for readability. Co-authored-by: Samuel Berger <[email protected]> Co-authored-by: Michael Squires <[email protected]> Co-authored-by: Brad Litterell <[email protected]> Co-authored-by: opbld17 <[email protected]> Co-authored-by: Colin Robertson <[email protected]> Co-authored-by: opbld16 <[email protected]> Co-authored-by: Kisaragi <[email protected]> Co-authored-by: Courtney Wales <[email protected]> Co-authored-by: opbld15 <[email protected]> Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: Bryan Gold <[email protected]> Co-authored-by: Edward Breeveld <[email protected]> Co-authored-by: Jeff Borsecnik <[email protected]> Co-authored-by: jsuther1974 <[email protected]> Co-authored-by: Austin Morton <[email protected]> Co-authored-by: Jak Koke <[email protected]> Co-authored-by: HO-COOH <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: James Barnett <[email protected]> Co-authored-by: Dennis Rea <[email protected]> Co-authored-by: Alex Buck <[email protected]> Co-authored-by: Tyler Whitney <[email protected]> Co-authored-by: ragingo <[email protected]> Co-authored-by: Feng Xu <[email protected]> Co-authored-by: Chuck Walbourn <[email protected]> Co-authored-by: Colin Cooper <[email protected]> Co-authored-by: Lychy <[email protected]> Co-authored-by: chausner <[email protected]> Co-authored-by: Stacyrch140 <[email protected]> Co-authored-by: Paula Miller <[email protected]> Co-authored-by: Stephanie Savell <[email protected]> Co-authored-by: Linda Spiller <[email protected]>
1 parent d2b1b57 commit 4e0c557

File tree

5 files changed

+112
-109
lines changed

5 files changed

+112
-109
lines changed

docs/code-quality/c6200.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
---
22
description: "Learn more about: C6200"
33
title: C6200
4-
ms.date: 11/04/2016
4+
ms.date: 08/16/2022
55
ms.topic: reference
6-
f1_keywords: ["C6200"]
6+
f1_keywords: ["C6200", "INDEX_EXCEEDS_MAX_NONSTACK", "__WARNING_INDEX_EXCEEDS_MAX_NONSTACK"]
77
helpviewer_keywords: ["C6200"]
88
ms.assetid: bbeb159b-4e97-4317-9a07-bb83cd03069a
99
---
10-
# C6200
10+
# Warning C6200
1111

12-
> warning C6200: index \<name> is out of valid index range \<min> to \<max> for non-stack buffer \<variable>
12+
> Index '*index*' is out of valid index range '*min*' to '*max*' for non-stack buffer '*parameter-name*'
1313
14-
This warning indicates that an integer offset into the specified array exceeds the maximum bounds of that array. This defect might cause random behavior or crashes.
14+
This warning indicates that an integer offset into the specified non-stack array exceeds the maximum bounds of that array, potentially causing random behavior and/or crashes.
15+
16+
## Remarks
1517

1618
One common cause of this defect is using the size of an array as an index into the array. Because C/C++ array indexing is zero-based, the maximum legal index into an array is one less than the number of array elements.
1719

20+
Code analysis name: INDEX_EXCEEDS_MAX_NONSTACK
21+
1822
## Example
1923

20-
The following code generates this warning because the **`for`** loop exceeds the index range:
24+
The following code generates this warning. This issue stems from the **`for`** loop exceeding the index range, attempting to access index 14 (the 15th element) when index 13 (the 14th element) is the last:
2125

2226
```cpp
23-
int buff[14]; // array of 0..13 elements
2427
void f()
2528
{
26-
for (int i=0; i<=14;i++) // i exceeds the index
27-
{
28-
buff[i]= 0; // warning C6200
29-
// code...
30-
}
29+
int* buff = new int[14]; // array of 0..13 elements
30+
for (int i = 0; i <= 14; i++) // i exceeds the index
31+
{
32+
buff[i] = 0; // warning C6200
33+
}
34+
delete[] buff;
3135
}
3236
```
3337

3438
To correct both warnings, use correct array size as shown in the following code:
3539

3640
```cpp
37-
int buff[14]; // array of 0..13 elements
3841
void f()
3942
{
40-
for ( int i=0; i < 14; i++) // loop stops when i < 14
41-
{
42-
buff[i]= 0; // initialize buffer
43-
// code...
44-
}
43+
int* buff = new int[14]; // array of 0..13 elements
44+
for (int i = 0; i < 14; i++) // i == 13 on the final iteration
45+
{
46+
buff[i] = 0; // initialize buffer
47+
}
48+
delete[] buff;
4549
}
4650
```

docs/code-quality/c6201.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
---
2-
description: "Learn more about: C6201"
3-
title: C6201
4-
ms.date: 11/04/2016
2+
description: "Learn more about: Warning C6201"
3+
title: Warning C6201
4+
ms.date: 09/28/2022
55
ms.topic: reference
6-
f1_keywords: ["C6201"]
6+
f1_keywords: ["C6201", "INDEX_EXCEEDS_MAX", "__WARNING_INDEX_EXCEEDS_MAX"]
77
helpviewer_keywords: ["C6201"]
88
ms.assetid: eefbbd77-007c-4f28-95f6-6de5ee6a27db
99
---
10-
# C6201
10+
# Warning C6201
1111

12-
> warning C6201: buffer overrun for \<variable>, which is possibly stack allocated: index \<name> is out of valid index range \<min> to \<max>
12+
> Index '*index-name*' is out of valid index range '*minimum*' to '*maximum*' for possibly stack allocated buffer '*variable*'
1313
14-
This warning indicates that an integer offset into the specified stack array exceeds the maximum bounds of that array. This defect might cause random behavior or crashes.
14+
This warning indicates that an integer offset into the specified stack array exceeds the maximum bounds of that array. It may potentially cause stack overflow errors, random behavior, or crashes.
15+
16+
## Remarks
1517

1618
One common cause of this defect is using an array's size as an index into the array. Because C/C++ array indexing is zero-based, the maximum legal index into an array is one less than the number of array elements.
1719

20+
Code analysis name: INDEX_EXCEEDS_MAX
21+
1822
## Example
1923

20-
The following code generates this warning because the array index is out of the valid range:
24+
The following code generates warning C6201. The **`for`** loop condition exceeds the valid index range for `buff` when it sets `i` to 14, which is one element past the end:
2125

2226
```cpp
23-
void f( )
27+
void f()
2428
{
25-
int buff[25];
26-
for (int i=0; i <= 25; i++) // i exceeds array bound
27-
{
28-
buff[i]=0; // initialize i
29-
// code ...
30-
}
29+
int buff[14]; // array of 0..13 elements
30+
for (int i = 0; i <= 14; i++) // i == 14 exceeds the bounds
31+
{
32+
buff[i] = 0; // initialize buffer
33+
}
3134
}
3235
```
3336

34-
To correct both warnings, use the correct array size as shown in the following code:
37+
To correct the warning, make sure the index stays in bounds. The following code shows the corrected loop condition:
3538

3639
```cpp
37-
void f( )
40+
void f()
3841
{
39-
int buff[25];
40-
for (int i=0; i < 25; i++)
41-
{
42-
buff[i]=0; // initialize i
43-
// code ...
44-
}
42+
int buff[14]; // array of 0..13 elements
43+
for (int i = 0; i < 14; i++) // i == 13 on the final iteration
44+
{
45+
buff[i]= 0; // initialize buffer
46+
}
4547
}
4648
```

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
```

docs/code-quality/c6277.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
---
22
description: "Learn more about: C6277"
33
title: C6277
4-
ms.date: 11/04/2016
4+
ms.date: 09/28/2022
55
ms.topic: reference
6-
f1_keywords: ["C6277"]
6+
f1_keywords: ["C6277", "CREATEPROCESS_ESCAPE", "__WARNING_CREATEPROCESS_ESCAPE"]
77
helpviewer_keywords: ["C6277"]
88
ms.assetid: 2b41252a-68c2-4e92-b005-0458db5f4430
99
---
10-
# C6277
10+
# Warning C6277
1111

12-
> warning C6277: NULL application name with an unquoted path in call to \<function>: results in a security vulnerability if the path contains spaces
12+
> NULL application name with an unquoted path in call to '*function-name*': results in a security vulnerability if the path contains spaces
1313
14-
This warning indicates that the application name parameter is null and there might be spaces in the executable path name. In this case, unless the executable name is "fully qualified," there is likely to be a security problem. A malicious user might insert a rogue executable with the same name earlier in the path. To correct this warning, you can specify the application name instead of passing null or if you do pass null for the application name, use quotation marks around the executable path.
14+
This warning indicates that the application name parameter is null and that there might be spaces in the executable path name.
15+
16+
## Remarks
17+
18+
Unless the executable name is fully qualified, there's likely to be a security problem. A malicious user could insert a rogue executable with the same name earlier in the path. To correct this warning, you can specify the application name instead of passing null. Alternatively, if you do pass null for the application name, use quotation marks around the executable path.
19+
20+
Code analysis name: CREATEPROCESS_ESCAPE
1521

1622
## Example
1723

18-
The following sample code generates this warning because the application name parameter is null, and the executable path name has a space in it; there is a risk that a different executable could be run because of the way the function parses spaces. For more information, see [CreateProcess](/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa).
24+
The following sample code generates warning C6277. The warning is caused by the NULL application name and from the executable path name having a space. Due to how the function parses spaces, there's a risk that a different executable could be run. For more information, see [`CreateProcessA`](/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa).
1925

2026
```cpp
2127
#include <windows.h>

docs/code-quality/c6308.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
---
2-
title: C6308
2+
title: Warning C6308
33
description: "Understand the causes of Microsoft C/C++ code analysis warning C6308, and learn how to fix them."
4-
ms.date: 10/23/2020
4+
ms.date: 09/28/2022
55
ms.topic: reference
6-
f1_keywords: ["C6308"]
6+
f1_keywords: ["C6308", "REALLOCLEAK", "__WARNING_REALLOCLEAK"]
77
helpviewer_keywords: ["C6308"]
88
ms.assetid: 1162cd96-9037-4576-9858-0c8361a12559
99
---
10-
# C6308
10+
# Warning C6308
1111

12-
> warning C6308: 'realloc' may return null pointer: assigning a null pointer to \<variable>, which is passed as an argument to 'realloc', will cause the original memory block to be leaked
12+
> 'realloc' may return null pointer: assigning a null pointer to '*parameter-name*', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
1313
14-
This warning indicates a memory leak that is the result of the incorrect use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.
14+
## Remarks
15+
16+
Heap reallocation functions don't free the passed buffer if reallocation is unsuccessful, potentially resulting in a memory leak if not handled properly. To correct the issue, assign the result of the reallocation function to a temporary variable, and then replace the original pointer after successful reallocation.
17+
18+
Code analysis name: REALLOCLEAK
1519

1620
## Example
1721

18-
The following sample code generates this warning:
22+
The following sample code generates warning C6308. This issue stems from the assignment of the return value from `realloc` to `x`. If `realloc` fails and returns a null pointer, then the original memory pointed to by `x` won't be freed:
1923

2024
```cpp
2125
#include <malloc.h>
2226
#include <windows.h>
2327

2428
void f( )
2529
{
26-
char *x;
27-
x = (char *) malloc(10);
28-
if (x != NULL)
29-
{
30-
x = (char *) realloc(x, 512);
31-
// code...
32-
free(x);
33-
}
30+
char *x = (char *) malloc(10);
31+
if (x != NULL)
32+
{
33+
x = (char *) realloc(x, 512);
34+
// code...
35+
free(x);
36+
}
3437
}
3538
```
3639
37-
To correct this warning, use the following code:
40+
To resolve the issue, you can create a temporary variable to store the return value of `realloc`. This change allows you to free the previously allocated memory safely if `realloc` fails:
3841
3942
```cpp
4043
#include <malloc.h>
4144
#include <windows.h>
4245
4346
void f()
4447
{
45-
char *x, *tmp;
46-
47-
x = (char *) malloc(10);
48-
49-
if (x != NULL)
50-
{
51-
tmp = (char *) realloc(x,512);
52-
if (tmp != NULL)
48+
char *x = (char *) malloc(10);
49+
if (x != NULL)
5350
{
54-
x = tmp;
51+
char *tmp = (char *) realloc(x,512);
52+
if (tmp != NULL)
53+
{
54+
x = tmp;
55+
}
56+
// code...
57+
free(x);
5558
}
56-
// code...
57-
free(x);
58-
}
5959
}
6060
```
6161

62-
This warning might generate noise if there is a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
62+
This warning might generate noise if there's a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
6363

64-
To avoid these kinds of problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [shared_ptr](../standard-library/shared-ptr-class.md), [unique_ptr](../standard-library/unique-ptr-class.md), and [vector](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
64+
To avoid these kinds of issues altogether, you can use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [`shared_ptr`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and [`vector`](../standard-library/vector.md). For more information, see [Smart pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
6565

6666
## See also
6767

68-
[C6014](../code-quality/c6014.md)
68+
[Warning C6014](../code-quality/c6014.md)

0 commit comments

Comments
 (0)