Skip to content

Commit b0565fc

Browse files
authored
Merge pull request MicrosoftDocs#4505 from MugBergerFries/patch-10
Updated C6383, C28159, C28160, C28213, C33010
2 parents 1391d78 + d2358af commit b0565fc

File tree

5 files changed

+82
-74
lines changed

5 files changed

+82
-74
lines changed

docs/code-quality/c28159.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
---
22
description: "Learn more about: C28159"
33
title: C28159
4-
ms.date: 11/04/2016
4+
ms.date: 09/08/2022
55
ms.topic: reference
6-
f1_keywords: ["C28159"]
6+
f1_keywords: ["C28159", "USE_OTHER_FUNCTION", "__WARNING_USE_OTHER_FUNCTION"]
77
helpviewer_keywords: ["C28159"]
88
ms.assetid: fab6cd58-0985-4ef6-89a2-64ed04297437
99
---
10-
# C28159
10+
# Warning C28159
1111

12-
> warning C28159: Consider using another function instead.
12+
> Consider using `*function_name_1*` instead of `*function_name_2*`. Reason: *reason*
1313
14-
This warning is reported for Drivers is suggesting that you use a preferred function call that is semantically equivalent to the function that the driver is calling. This is a general warning message; the annotation `__drv_preferredFunction` was used (possibly with a conditional a `__drv_when`() annotation) to flag a bad coding practice.
14+
This warning occurs when you use a function that is semantically equivalent to an alternative, preferred function call.
15+
16+
## Remarks
17+
18+
C28159 is a general warning message; the annotation `__drv_preferredFunction` was used (possibly with a conditional `__drv_when`() annotation) to flag a bad coding practice.
19+
20+
Code analysis name: USE_OTHER_FUNCTION
1521

1622
## Example
1723

18-
The following code example generates this warning:
24+
The following code example generates this warning. This issue is due to the use of `OemToChar`, which doesn't validate the buffer size:
1925

2026
```cpp
2127
char buff[MAX_PATH];
22-
23-
// if strlen(input) > MAX_PATH
24-
// leads to buffer overrun
25-
OemToChar(buff, input);
28+
OemToChar(buff, input); // If strlen(input) > MAX_PATH, this call leads to buffer overrun
2629
```
2730
28-
The following code example avoids this warning:
31+
The following code example avoids this warning by using the recommended alternative `OemToCharBuff`, which takes in the destination buffer size and limits the copy appropriately:
2932
3033
```cpp
3134
char buff[MAX_PATH];
32-
3335
OemToCharBuff(buff, input, MAX_PATH);
3436
```

docs/code-quality/c28160.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
---
22
description: "Learn more about: C28160"
33
title: C28160
4-
ms.date: 11/04/2016
4+
ms.date: 09/08/2022
55
ms.topic: reference
6-
f1_keywords: ["C28160"]
6+
f1_keywords: ["C28160", "ERROR", "__WARNING_ERROR"]
77
helpviewer_keywords: ["C28160"]
88
ms.assetid: cab15f6b-909c-4cc8-81a0-c24ac7c91c7c
99
---
10-
# C28160
10+
# Warning C28160
1111

12-
> warning C28160: Error annotation
12+
> Error annotation: *reason*
1313
14-
This warning is reported when a `__drv_error` annotation has been encountered. This annotation is used to flag coding practices that should be fixed, and can be used with a `__drv_when` annotation to indicate specific combinations of parameters.
14+
This warning is reported when a `__drv_error` annotation has been encountered.
15+
16+
## Remarks
17+
18+
This annotation is used to flag coding practices that should be fixed, and can be used with a `__drv_when` annotation to indicate specific combinations of parameters.
19+
20+
Code analysis name: ERROR

docs/code-quality/c28213.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
11
---
22
description: "Learn more about: C28213"
33
title: C28213
4-
ms.date: 11/04/2016
4+
ms.date: 09/08/2022
55
ms.topic: reference
6-
f1_keywords: ["C28213"]
6+
f1_keywords: ["C28213", "BAD_USEHEADER", "__WARNING_BAD_USEHEADER"]
77
helpviewer_keywords: ["C28213"]
88
ms.assetid: e141a12a-4c46-47eb-aa9d-a6444472cfaa
99
---
10-
# C28213
10+
# Warning C28213
1111

12-
> warning C28213: The `_Use_decl_annotations_` annotation must be used to reference, without modification, a prior declaration.
12+
> The `_Use_decl_annotations_` annotation must be used to reference, without modification, a prior declaration.
1313
14-
`_Use_decl_annotations_` tells the compiler to use the annotations from an earlier declaration of the function. If no earlier declaration can be found, or if the current declaration makes changes to the annotations than this warning is emitted.
14+
## Remarks
15+
16+
`_Use_decl_annotations_` tells the compiler to use the annotations from an earlier declaration of the function. If no earlier declaration can be found, or if the current declaration makes changes to the annotations, then this warning is emitted.
17+
18+
Code analysis name: BAD_USEHEADER
1519

1620
## Example
1721

22+
The following code generates C28160. The `buffer` parameter annotation doesn't match between the two files.
23+
24+
*From example.h:*
25+
1826
```cpp
19-
// from example.h
2027
void example_func(_Out_writes_(n) char* buffer, int n);
28+
```
29+
30+
*From example.cpp:*
2131
22-
// from example.cpp
32+
```cpp
2333
_Use_decl_annotations_
2434
void example_func(_Out_writes_z_(n) char* buffer, int n)
2535
{
26-
// ...
27-
buffer[n] = '\0';
36+
buffer[n] = '\0';
2837
}
2938
```
3039

31-
The `buffer` parameter annotation does not match between the two files. This can be fixed by either changing the annotation so they match at all locations, or by removing all annotations except `_Use_decl_annotations_` from the function definition. In this example `_Out_writes_z_` appears to be correct so we will move that to the function declaration in the header file.
40+
This issue can be fixed by either changing the annotation so they match at all locations, or by removing all annotations except `_Use_decl_annotations_` from the function definition. In this example, `_Out_writes_z_` appears to be correct so we'll move that to the function declaration in the header file. The following code resolves this warning:
3241

33-
```cpp
42+
*From example.h:*
3443

35-
// from example.h
44+
```cpp
3645
void example_func(_Out_writes_z_(n) char* buffer, int n);
46+
```
3747
38-
// from example.cpp
48+
*From example.cpp:*
49+
50+
```cpp
3951
_Use_decl_annotations_
40-
void example_func(char* buffer, int n)
52+
void example_func(_Out_writes_z_(n) char* buffer, int n)
4153
{
42-
// ...
43-
buffer[n] = '\0';
54+
buffer[n] = '\0';
4455
}
4556
```

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 isn't out of range. Because an enum variable is signed by default, it can have a negative value. A negative array index 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+
The following code remediates this warning by checking the lower bound as well to ensure `idx` isn't 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

docs/code-quality/c6383.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,41 @@
11
---
22
description: "Learn more about: C6383"
33
title: C6383
4-
ms.date: 11/04/2016
4+
ms.date: 09/07/2022
55
ms.topic: reference
6-
f1_keywords: ["C6383"]
6+
f1_keywords: ["C6383", "ELEMENTS_TO_BYTES", "__WARNING_ELEMENTS_TO_BYTES"]
77
helpviewer_keywords: ["C6383"]
88
ms.assetid: f5ff7938-0fbe-4b71-b98f-098fe887799d
99
---
10-
# C6383
10+
# Warning C6383
1111

12-
> warning C6383: buffer overrun due to conversion of an element count into a byte count: an element count is expected for parameter \<number> in call to \<function>
12+
> Buffer overrun due to conversion of an element count into a byte count: an element count is expected for parameter `*parameter_name*` in call to `*function_name*`
1313
14-
This warning indicates that a non-constant byte count is being passed when an element count is required. Typically, this occurs when a variable is multiplied by the **`sizeof`** a type, but code analysis suggests that an element count is required.
14+
This warning indicates that a non-constant byte count is being passed when an element count is instead required.
1515

16-
## Example
16+
## Remarks
1717

18-
The following code generates this warning:
18+
Typically, this warning occurs when a variable is multiplied by the `sizeof` a type. This issue will likely result in more bytes being copied to the buffer than it can hold.
1919

20-
```cpp
21-
#include <string.h>
20+
Code analysis name: ELEMENTS_TO_BYTES
2221

23-
void f( wchar_t* t, wchar_t* s, int n )
24-
{
25-
// code ...
26-
wcsncpy (t, s, n*sizeof(wchar_t)); // warning C6383
27-
// code ...
28-
}
29-
```
22+
## Example
3023

31-
To correct this warning, do not multiply the variable with the **`sizeof`** a type as shown in the following code:
24+
The following code generates this warning. `wcsncpy` will allow n \* sizeof(wchar_t) characters to be copied, but the buffer can only hold n characters. It should be noted that `wcsncpy` is an unsafe function, and shouldn't be used per [C28719](/windows-hardware/drivers/devtest/28719-banned-api-usage-use-updated-function-replacement). The unsafe variant is used here only for the purposes of demonstrating this warning:
3225

3326
```cpp
34-
void f( wchar_t* t, wchar_t* s, int n )
27+
void f(wchar_t* t, wchar_t* s, int n)
3528
{
36-
// code ...
37-
wcsncpy (t, s, n);
38-
// code ...
29+
wcsncpy (t, s, n*sizeof(wchar_t));
3930
}
4031
```
4132
42-
The following code corrects this warning by using the safe string manipulation function:
33+
The following code corrects this warning by sending element count instead of the byte count:
4334
4435
```cpp
45-
void f(wchar_t* t, wchar_t* s, size_t n)
36+
void f( wchar_t* t, wchar_t* s, int n )
4637
{
47-
// code ...
48-
wcsncpy_s( t, sizeof(s), s, n );
49-
// code ...
38+
wcsncpy (t, s, n);
5039
}
5140
```
5241

0 commit comments

Comments
 (0)