Skip to content

Commit aa5da93

Browse files
authored
Merge pull request #4921 from MicrosoftDocs/main
5/22 AM Publish
2 parents fe86c3c + 2fae89e commit aa5da93

File tree

5 files changed

+242
-0
lines changed

5 files changed

+242
-0
lines changed

docs/code-quality/c26831.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
title: Warning C26831
3+
description: "Describes the Microsoft C/C++ code analysis warning C26831, its causes, and how to address it."
4+
ms.date: 03/20/2023
5+
f1_keywords: ["C26831", "ALLOCATION_POTENTIAL_OVERFLOW"]
6+
helpviewer_keywords: ["C26831"]
7+
---
8+
# Warning `C26831`
9+
10+
> Allocation size might be the result of a numerical overflow
11+
12+
## Remarks
13+
14+
This warning reports that the size specified for an allocation may be the result of a numerical overflow. For example:
15+
16+
```cpp
17+
void *SmallAlloc(int);
18+
19+
void foo(int i, int j)
20+
{
21+
int* p = (int*)SmallAlloc(i + j); // Warning: C26831
22+
p[i] = 5;
23+
}
24+
```
25+
26+
If `i+j` overflows, `SmallAlloc` returns a buffer that is smaller than expected. That will likely lead to out of bounds attempts to access the buffer later on. This code pattern can result in remote code execution vulnerabilities.
27+
28+
The check applies to common allocation functions like `new`, `malloc`, and `VirtualAlloc`. The check also applies to custom allocator functions that have `alloc` (case insensitive) in the function name.
29+
30+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
31+
32+
This warning is available in Visual Studio 2022 version 17.7 and later versions.
33+
34+
## Example
35+
36+
To fix the previous code example in which `i+j` might overflow, introduce a check to make sure it won't. For example:
37+
38+
```cpp
39+
void *SmallAlloc(int);
40+
41+
void foo(int i, int j)
42+
{
43+
if (i < 0 || j < 0 )
44+
{
45+
return;
46+
}
47+
48+
if (i > 100 || j > 100)
49+
{
50+
return;
51+
}
52+
53+
int* p = (int*)SmallAlloc(i + j);
54+
p[i] = 5;
55+
}
56+
```
57+
58+
## See also
59+
60+
[`C26832`](c26832.md)\
61+
[`C26833`](c26833.md)

docs/code-quality/c26832.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Warning C26832
3+
description: "Describes the Microsoft C/C++ code analysis warning C26832, its causes, and how to address it."
4+
ms.date: 03/20/2023
5+
f1_keywords: ["C26832", "ALLOCATION_POTENTIAL_OVERFLOW_AFTER_CAST"]
6+
helpviewer_keywords: ["C26832"]
7+
---
8+
# Warning `C26832`
9+
10+
> Allocation size is the result of a narrowing conversion that could result in overflow
11+
12+
## Remarks
13+
14+
This warning reports that the size specified for an allocation may be the result of a narrowing conversion that results in a numerical overflow. For example:
15+
16+
```cpp
17+
void* SmallAlloc(int);
18+
19+
void foo(unsigned short i, unsigned short j)
20+
{
21+
unsigned short size = i + j;
22+
23+
int* p = (int*)SmallAlloc(size); // Warning: C26832
24+
p[i] = 5;
25+
}
26+
```
27+
28+
In the expression `i + j`, both `i` and `j` are promoted to integers, and the result of the addition is stored in a temporary integer. Then, the temporary integer is implicitly cast to an `unsigned short` before the value is stored in `size`. The cast to `unsigned short` might overflow, in which case `SmallAlloc` may return a smaller buffer than expected. That will likely lead to out of bounds attempts to access the buffer later on. This code pattern can result in remote code execution vulnerabilities
29+
30+
This check applies to common allocation functions like `new`, `malloc`, and `VirtualAlloc`. The check also applies to custom allocator functions that have `alloc` (case insensitive) in the function name.
31+
32+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
33+
34+
This warning is available in Visual Studio 2022 version 17.7 and later versions.
35+
36+
## Example
37+
38+
To fix the previous code example in which `i+j` might overflow, introduce a check to make sure it won't. For example:
39+
40+
```cpp
41+
void *SmallAlloc(int);
42+
43+
void foo(unsigned short i, unsigned short j)
44+
{
45+
if (i > 100 || j > 100)
46+
return;
47+
48+
unsigned short size = i + j;
49+
50+
int* p = (int*)SmallAlloc(size);
51+
p[i] = 5;
52+
}
53+
```
54+
55+
## See also
56+
57+
[`C26831`](c26831.md)\
58+
[`C26833`](c26833.md)

docs/code-quality/c26833.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Warning C26833
3+
description: "Describes the Microsoft C/C++ code analysis warning C26833, its causes, and how to address it."
4+
ms.date: 03/20/2023
5+
f1_keywords: ["C26833", "ALLOCATION_POTENTIAL_OVERFLOW_BEFORE_CHECK"]
6+
helpviewer_keywords: ["C26833"]
7+
---
8+
# Warning `C26833`
9+
10+
> Allocation size might be the result of a numerical overflow before the bound check
11+
12+
## Remarks
13+
14+
This warning reports that the size specified for an allocation may be the result of a numerical overflow. For example:
15+
16+
```cpp
17+
void* SmallAlloc(int);
18+
19+
void foo(unsigned i, unsigned j)
20+
{
21+
unsigned size = i + j;
22+
23+
if (size > 50)
24+
{
25+
return;
26+
}
27+
28+
int* p = (int*)SmallAlloc(size + 5); // Warning: C26833
29+
p[j] = 5;
30+
}
31+
```
32+
33+
The check for `size > 50` is too late. If `i + j` overflows, it produces a small value that passes the check. Then, `SmallAlloc` allocates a buffer smaller than expected. That will likely lead to out of bounds attempts to access the buffer later on. This code pattern can result in remote code execution vulnerabilities.
34+
35+
This check applies to common allocation functions like `new`, `malloc`, and `VirtualAlloc`. The check also applies to custom allocator functions that have `alloc` (case insensitive) in the function name.
36+
37+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
38+
39+
This warning is available in Visual Studio 2022 version 17.7 and later versions.
40+
41+
## Example
42+
43+
To fix the previous code example, make sure `i+j` can't overflow. For example:
44+
45+
```cpp
46+
void* SmallAlloc(int);
47+
48+
void foo(unsigned i, unsigned j)
49+
{
50+
if (i > 100 || j > 100)
51+
{
52+
return;
53+
}
54+
55+
unsigned size = i + j;
56+
57+
if (size > 50)
58+
{
59+
return;
60+
}
61+
62+
int* p = (int*)SmallAlloc(size + 5);
63+
p[j] = 5;
64+
}
65+
```
66+
67+
## See also
68+
69+
[`C26831`](c26831.md)\
70+
[`C26832`](c26832.md)

docs/code-quality/c26835.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Warning C26835
3+
description: "Describes the Microsoft C/C++ code analysis warning C26835, its causes, and how to address it."
4+
ms.date: 03/20/2023
5+
f1_keywords: ["C26835", "RTL_COMPARE_MEMORY_MISUSE"]
6+
helpviewer_keywords: ["C26835"]
7+
---
8+
# Warning `C26835`
9+
10+
> `RtlCompareMemory` returns the number of matching bytes. Consider replacing this call with `RtlEqualMemory`
11+
12+
## Remarks
13+
14+
When `RtlCompareMemory`'s return value is treated as a boolean, it evaluates to true when there is at least 1 equal byte before finding a difference. Moreover, comparing the result of `RtlCompareMemory` to 0 evaluates to false if there is at least 1 matching byte. This behavior may be unexpected because it's different from other comparison functions such as `strcmp`, making the code harder to understand. To check for equality, consider using `RtlEqualMemory` instead.
15+
16+
This warning is available in Visual Studio 2022 version 17.7 and later versions.
17+
18+
## Example
19+
20+
```cpp
21+
int foo(const void* ptr)
22+
{
23+
if (RtlCompareMemory("test", ptr, 5) == 0) // C26835
24+
{
25+
// ...
26+
}
27+
}
28+
```
29+
30+
To fix the issue, verify if the original intention was to check for equality and replace the function call with `RtlEqualMemory`:
31+
32+
```cpp
33+
int foo(const void* ptr)
34+
{
35+
if (RtlEqualMemory("test", ptr, 5)) // C26835
36+
{
37+
// ...
38+
}
39+
}
40+
```
41+
42+
## See also
43+
44+
[`RtlEqualMemory` macro (`wdm.h`)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlequalmemory)\
45+
[`RtlCompareMemory` function (`wdm.h`)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlcomparememory)

docs/code-quality/toc.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ items:
611611
href: ../code-quality/c26829.md
612612
- name: Warning C26830
613613
href: ../code-quality/c26830.md
614+
- name: Warning C26831
615+
href: ../code-quality/c26831.md
616+
- name: Warning C26832
617+
href: ../code-quality/c26832.md
618+
- name: Warning C26833
619+
href: ../code-quality/c26833.md
620+
- name: Warning C26835
621+
href: ../code-quality/c26835.md
614622
- name: Warning C26859
615623
href: ../code-quality/c26859.md
616624
- name: Warning C26860

0 commit comments

Comments
 (0)