Skip to content

Commit 5c7ddd5

Browse files
Updated C33010
Updated to new format
1 parent 944d587 commit 5c7ddd5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

docs/code-quality/c33010.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ description: C33010 warning for enums
44
keywords: c33010
55
author: hwisungi
66
ms.author: hwisungi
7-
ms.date: 06/20/2020
7+
ms.date: 09/08/2022
88
ms.topic: reference
9-
f1_keywords: ["C33010"]
9+
f1_keywords: ["C33010", "UNCHECKED_LOWER_BOUND_FOR_ENUMINDEX", "__WARNING_UNCHECKED_LOWER_BOUND_FOR_ENUMINDEX"]
1010
helpviewer_keywords: ["C33010"]
1111
dev_langs: ["C++"]
1212
---
13-
# C33010
13+
# Warning C33010
1414

15-
> Warning C33010: Unchecked lower bound for enum 'enum' used as index.
15+
> Unchecked lower bound for enum '\**enum_name*' used as index.
1616
17-
This warning is triggered for an enum that is used as an index into an array,
18-
if the upper bound is checked for its value, but not the lower bound.
17+
This warning is triggered if an enum is both used as an index into an array and isn't checked on the lower bound.
18+
19+
## Remarks
20+
21+
Code using enumerated types as indexes for arrays will often check for the upper bound in order to ensure the index is not out of range. Because an enum variable is signed by default, it can have a negative value. If it is used as an index into an array of values or an array of function pointers, a negative value can allow arbitrary memory to be read, used, or even executed.
22+
23+
Code analysis name: UNCHECKED_LOWER_BOUND_FOR_ENUMINDEX
1924

2025
## Example
2126

22-
Code using enumerated types as indexes for arrays will often check for the upper bound
23-
in order to ensure the index is not out of range. Because an enum variable is signed by default,
24-
it can have a negative value. If it is used as an index into an array of values or an array of function pointers,
25-
a negative value can allow arbitrary memory to be read, used, or even executed.
27+
The following code generates this warning. `idx` is used as an index to access `functions`, but the lower bound is never checked.
2628

2729
```cpp
2830
typedef void (*PFN)();
@@ -41,14 +43,13 @@ void foo(Index idx, PFN(&functions)[5])
4143
if (idx > Index::Max)
4244
return;
4345

44-
auto pfn = functions[static_cast<int>(idx)]; // C33010
46+
auto pfn = functions[static_cast<int>(idx)];
4547
if (pfn != nullptr)
4648
(*pfn)();
47-
// ......
4849
}
4950
```
5051
51-
These warnings are corrected by checking the index value for lower bound as well:
52+
These following code remediates this warning by checking the lower bound as well to ensure `idx` is not negative:
5253
5354
```cpp
5455
typedef void (*PFN)();
@@ -67,10 +68,9 @@ void foo(Index idx, PFN(&functions)[5])
6768
if (idx < Index::Zero || idx > Index::Max)
6869
return;
6970
70-
auto pfn = functions[static_cast<int>(idx)]; // OK
71+
auto pfn = functions[static_cast<int>(idx)];
7172
if (pfn != nullptr)
7273
(*pfn)();
73-
// ......
7474
}
7575
```
7676

0 commit comments

Comments
 (0)