Skip to content

Commit 8751727

Browse files
committed
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents f79ac8a + 4e0c557 commit 8751727

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)