Skip to content

Commit 603fe2c

Browse files
authored
Merge pull request MicrosoftDocs#3120 from hwisungi/master
VC Code Analysis - adding help docs for new rules added in 16.8
2 parents f94b93b + 4372900 commit 603fe2c

File tree

8 files changed

+432
-0
lines changed

8 files changed

+432
-0
lines changed

docs/code-quality/c33001.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: c33001
3+
description: C33001 warning for VARIANTs
4+
keywords: c33001
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33001"]
10+
helpviewer_keywords: ["C33001"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33001
14+
15+
> Warning C33001: VARIANT 'var' was cleared when it was uninitialized (expression 'expr')
16+
17+
This warning is triggered when an uninitialized VARIANT is passed into an API such as VariantClear
18+
that expects an initialized VARIANT.
19+
20+
## Example
21+
22+
```cpp
23+
#include <Windows.h>
24+
25+
HRESULT foo(bool some_condition)
26+
{
27+
VARIANT var;
28+
29+
if (some_condition)
30+
{
31+
//...
32+
VariantInit(&var);
33+
//...
34+
}
35+
36+
VariantClear(&var); // C33001
37+
}
38+
```
39+
40+
These warnings are corrected by ensuring VariantClear is called only for a properly initialized VARIANT:
41+
```cpp
42+
#include <Windows.h>
43+
44+
HRESULT foo(bool some_condition)
45+
{
46+
VARIANT var;
47+
48+
if (some_condition)
49+
{
50+
//...
51+
VariantInit(&var);
52+
//...
53+
VariantClear(&var); // C33001
54+
}
55+
}
56+
```
57+
## See also
58+
59+
[C33004](/cpp/code-quality/c33004)
60+
[C33005](/cpp/code-quality/c33005)

docs/code-quality/c33004.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: c33004
3+
description: C33004 warning for VARIANTs
4+
keywords: c33004
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33004"]
10+
helpviewer_keywords: ["C33004"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33004
14+
15+
> Warning C33004: VARIANT 'var', which is marked as _Out_ was cleared before being initialized (expression 'expr')
16+
17+
This warning is triggered when a VARIANT parameter with \_Out\_ SAL annotation, which may not be
18+
to be initialized on input, is passed to an API such as VariantClear that expects an initialized VARIANT.
19+
20+
## Example
21+
22+
```cpp
23+
#include <Windows.h>
24+
25+
void t2(_Out_ VARIANT* pv)
26+
{
27+
// ......
28+
VariantClear(pv); // C33004
29+
// ......
30+
}
31+
```
32+
33+
These warnings are corrected by ensuring VariantClear is called only for a properly initialized VARIANT:
34+
```cpp
35+
#include <Windows.h>
36+
37+
void t2(_Out_ VARIANT* pv)
38+
{
39+
VariantInit(pv);
40+
// ......
41+
VariantClear(pv); // OK
42+
// ......
43+
}
44+
```
45+
## See also
46+
47+
[C33001](/cpp/code-quality/c33001)
48+
[C33005](/cpp/code-quality/c33005)

docs/code-quality/c33005.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: c33005
3+
description: C33005 warning for VARIANTs
4+
keywords: c33005
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33005"]
10+
helpviewer_keywords: ["C33005"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33005
14+
15+
> Warning C33005: VARIANT 'var' was provided as an input or input/output parameter but was not initialized (expression 'expr')
16+
17+
This warning is triggered when an uninitialized VARIANT is passed to a function as input only or input/output
18+
parameter - for example, a pass-by-refrence parameter without an \_Out\_ SAL annotation.
19+
20+
## Example
21+
22+
```cpp
23+
#include <Windows.h>
24+
25+
void bar(VARIANT* v); // v is assumed to be input/output
26+
27+
void foo()
28+
{
29+
VARIANT v;
30+
bar(&v); // C33005
31+
// ......
32+
VariantClear(&v); // OK, assumed to be initialized by bar
33+
}
34+
```
35+
36+
These warnings are corrected by ensuring to initialize the VARIANT before passing it to a function
37+
as input-only or input/output.
38+
39+
```cpp
40+
#include <Windows.h>
41+
42+
void bar(VARIANT* v); // v is assumed to be input/output
43+
44+
void foo()
45+
{
46+
VARIANT v;
47+
VariantInit(&v);
48+
bar(&v); // OK
49+
// ......
50+
VariantClear(&v); // OK, assumed to be initialized by bar
51+
}
52+
```
53+
## See also
54+
55+
[C33001](/cpp/code-quality/c33001)
56+
[C33004](/cpp/code-quality/c33004)

docs/code-quality/c33010.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: c33010
3+
description: C33010 warning for enums
4+
keywords: c33010
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33010"]
10+
helpviewer_keywords: ["C33010"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33010
14+
15+
> Warning C33010: Unchecked lower bound for enum 'enum' used as index.
16+
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.
19+
20+
## Example
21+
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.
26+
27+
```cpp
28+
typedef void (*PFN)();
29+
30+
enum class Index
31+
{
32+
Zero,
33+
One,
34+
Two,
35+
Three,
36+
Max
37+
};
38+
39+
void foo(Index idx, PFN(&functions)[5])
40+
{
41+
if (idx > Index::Max)
42+
return;
43+
44+
auto pfn = functions[static_cast<int>(idx)]; // C33010
45+
if (pfn != nullptr)
46+
(*pfn)();
47+
// ......
48+
}
49+
```
50+
These warnings are corrected by checking the index value for lower bound as well:
51+
52+
```cpp
53+
typedef void (*PFN)();
54+
55+
enum class Index
56+
{
57+
Zero,
58+
One,
59+
Two,
60+
Three,
61+
Max
62+
};
63+
64+
void foo(Index idx, PFN(&functions)[5])
65+
{
66+
if (idx < Index::Zero || idx > Index::Max)
67+
return;
68+
69+
auto pfn = functions[static_cast<int>(idx)]; // OK
70+
if (pfn != nullptr)
71+
(*pfn)();
72+
// ......
73+
}
74+
```
75+
76+
## See also
77+
78+
[C33011](/cpp/code-quality/c33011)

docs/code-quality/c33011.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: c33011
3+
description: C33011 warning for enums
4+
keywords: c33011
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33011"]
10+
helpviewer_keywords: ["C33011"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33011
14+
15+
> Warning C33011: Unchecked upper bound for enum 'enum' used as index.
16+
17+
This warning is triggered when the lower bound of the enum used as index into an array is checked,
18+
but the upper bound is not checked.
19+
20+
This warning is triggered for an enum that is used as an index into an array,
21+
if the lower bound is checked for its value, but not the upper bound.
22+
23+
24+
## Example
25+
26+
Code that uses enumerated types as indexes for arrays must check the enum value for both lower and
27+
upper bounds. If the enum value is checked only for the lower bound before used to index into an array of values
28+
or an array of function pointers, then it can allow arbitrary memory to be read, used, or even executed.
29+
30+
```cpp
31+
typedef void (*PFN)();
32+
33+
enum class Index
34+
{
35+
Zero,
36+
One,
37+
Two,
38+
Three,
39+
Max
40+
};
41+
42+
void foo(Index idx, PFN(&functions)[5])
43+
{
44+
if (idx < Index::Zero)
45+
return;
46+
47+
auto pfn = functions[static_cast<int>(idx)]; // C33011
48+
if (pfn != nullptr)
49+
(*pfn)();
50+
// ......
51+
}
52+
```
53+
These warnings are corrected by declaring the enum as enum class:
54+
55+
```cpp
56+
typedef void (*PFN)();
57+
58+
enum class Index
59+
{
60+
Zero,
61+
One,
62+
Two,
63+
Three,
64+
Max
65+
};
66+
67+
void foo(Index idx, PFN(&functions)[5])
68+
{
69+
if (idx < Index::Zero || idx > Index::Max)
70+
return;
71+
72+
auto pfn = functions[static_cast<int>(idx)]; // OK
73+
if (pfn != nullptr)
74+
(*pfn)();
75+
// ......
76+
}
77+
```
78+
79+
## See also
80+
81+
[C33010](/cpp/code-quality/c33010)

docs/code-quality/c33020.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: c33020
3+
description: C33020 warning for HRESULTs
4+
keywords: c33020
5+
author: hwisungi
6+
ms.author: hwisungi
7+
ms.date: 06/20/2020
8+
ms.topic: reference
9+
f1_keywords: ["C33020"]
10+
helpviewer_keywords: ["C33020"]
11+
dev_langs: ["C++"]
12+
---
13+
# C33020
14+
15+
> Warning C33020: Likely incorrect HRESULT usage detected.
16+
17+
This is high-confidence warning indicating that HRESULT-returning function returns FALSE or false.
18+
19+
## Example
20+
21+
```cpp
22+
#include <Windows.h>
23+
24+
HRESULT foo()
25+
{
26+
// ......
27+
return FALSE; // C33020
28+
}
29+
```
30+
31+
These warnings are corrected by using proper HRESULT value:
32+
```cpp
33+
#include <Windows.h>
34+
35+
HRESULT foo()
36+
{
37+
// ......
38+
return E_FAIL; // OK
39+
}
40+
```
41+
42+
## See also
43+
44+
[C33022](/cpp/code-quality/c33022)

0 commit comments

Comments
 (0)