Skip to content

Commit 86bd796

Browse files
author
Hwi-sung Im
committed
Code Analysis - add help documents for new TwC checkers for VS
1 parent 4292284 commit 86bd796

8 files changed

Lines changed: 458 additions & 0 deletions

File tree

docs/code-quality/c33001.md

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

docs/code-quality/c33004.md

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

docs/code-quality/c33005.md

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

docs/code-quality/c33006.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: c33006
3+
keywords: c33006
4+
author: hwisungi
5+
ms.author: hwisungi
6+
ms.date: 06/20/2020
7+
ms.topic: reference
8+
f1_keywords: ["C33006"]
9+
helpviewer_keywords: ["C33006"]
10+
dev_langs: ["C++"]
11+
---
12+
# C33006
13+
14+
> Warning C33006: VARIANT 'var' was provided as a parameter without SAL and may have been cleared when
15+
> it was not initialized. (expression 'expr')
16+
17+
This warning is triggered when unannotated VARIANT parameters are encountered. If a parameter is not
18+
annotated with \_In\_ or \_Inout\_ SAL annotation, it cannot be determined whether the parameter has
19+
been initialiszed.
20+
21+
## Example
22+
23+
```cpp
24+
#include <Windows.h>
25+
26+
void foo(VARIANT* v)
27+
{
28+
VariantClear(v); // C33006
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 foo(VARIANT* v)
38+
{
39+
VariantInit(v);
40+
VariantClear(v); // OK
41+
//......
42+
}
43+
```
44+
45+
## See also
46+
47+
[TBD](link to be added)

docs/code-quality/c33010.md

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

docs/code-quality/c33011.md

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

0 commit comments

Comments
 (0)