Skip to content

Commit 3aef369

Browse files
authored
Merge pull request #4435 from MugBergerFries/patch-4
Updated C6201
2 parents b60cb77 + 2c347d3 commit 3aef369

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

0 commit comments

Comments
 (0)