Skip to content

Commit 7e75cb8

Browse files
TylerMSFTTylerMSFT
authored andcommitted
edits
1 parent 5db1a91 commit 7e75cb8

File tree

4 files changed

+53
-43
lines changed

4 files changed

+53
-43
lines changed

docs/code-quality/c26831.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
title: Warning C26831
33
description: "Describes the Microsoft C/C++ code analysis warning C26831, its causes, and how to address it."
4-
ms.date: 03/06/2023
4+
ms.date: 03/20/2023
55
f1_keywords: ["C26831", "ALLOCATION_POTENTIAL_OVERFLOW"]
66
helpviewer_keywords: ["C26831"]
77
---
8-
# Warning C26831
8+
# Warning `C26831`
99

1010
> Allocation size might be the result of a numerical overflow
1111
1212
## Remarks
1313

14-
Checks for an allocation whose size may be the result of a numerical overflow. This check is targeted to find the following code pattern:
14+
This warning reports that the size specified for an allocation may be the result of a numerical overflow. For example:
1515

1616
```cpp
1717
void *SmallAlloc(int);
@@ -23,29 +23,32 @@ void foo(int i, int j)
2323
}
2424
```
2525
26-
In case `i+j` overflows, `SmallAlloc` returns a buffer that is smaller than expected. As a result, future accesses to the buffer like `p[i]` are out of bounds. These code patterns can result in remote code execution vulnerabilities.
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.
2727
28-
Common allocation functions like `new`, `malloc`, and `VirtualAlloc` are recognized. The check also tries to recognize custom allocator functions by case insensitive search for the `alloc` substring in the function name.
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.
2929
30-
Our analysis engine's numerical solver have some limitations reasoning about numerical overflows. As a result, this check uses some heuristics and sometimes fails to recognize that certain checks can prevent overflows.
31-
Usually, checking for a reasonable bound on the allocation should work.
30+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
3231
3332
This warning is available in Visual Studio 2022 version 17.7 and later versions.
3433
3534
## Example
3635
37-
To fix the previous code example, make sure `i+j` can't overflow. For example:
36+
To fix the previous code example in which `i+j` might overflow, introduce a check to make sure it won't. For example:
3837
3938
```cpp
4039
void *SmallAlloc(int);
4140
4241
void foo(int i, int j)
4342
{
4443
if (i < 0 || j < 0 )
44+
{
4545
return;
46-
46+
}
47+
4748
if (i > 100 || j > 100)
49+
{
4850
return;
51+
}
4952
5053
int* p = (int*)SmallAlloc(i + j);
5154
p[i] = 5;
@@ -54,5 +57,5 @@ void foo(int i, int j)
5457

5558
## See also
5659

57-
[C26832](c26832.md)\
58-
[C26833](c26833.md)
60+
[`C26832`](c26832.md)\
61+
[`C26833`](c26833.md)

docs/code-quality/c26832.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ ms.date: 03/06/2023
55
f1_keywords: ["C26832", "ALLOCATION_POTENTIAL_OVERFLOW_AFTER_CAST"]
66
helpviewer_keywords: ["C26832"]
77
---
8-
# Warning C26832
8+
# Warning `C26832`
99

1010
> Allocation size is the result of a narrowing conversion that could result in overflow
1111
1212
## Remarks
1313

14-
Checks for an allocation whose size may be the result of a numerical overflow. This check is targeted to find the following code pattern:
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:
1515

1616
```cpp
1717
void* SmallAlloc(int);
@@ -25,17 +25,17 @@ void foo(unsigned short i, unsigned short j)
2525
}
2626
```
2727
28-
In `i + j`, `i` and `j` are promoted to integers and the result of the addition is stored into a temporary integer. This integer is implicitly casted to `unsigned short` before the value is stored to `size`. The result of this cast might overflow. So, `SmallAlloc` might end up returning a buffer smaller than expected. Future accesses like `p[i]` are out of bounds. These code patterns can result in remote code execution vulnerabilities.
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
2929
30-
Common allocation functions like `new`, `malloc`, and `VirtualAlloc` are recognized. The check also tries to recognize custom allocator functions by case insensitive search for the `alloc` substring in the function name.
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.
3131
32-
Our analysis engine's numerical solver have some limitations reasoning about numerical overflows. As a result, this check uses some heuristics and sometimes fails to recognize that certain checks can prevent overflows.
33-
Usually, checking for a reasonable bound on the allocation should work.
32+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
3433
3534
This warning is available in Visual Studio 2022 version 17.7 and later versions.
35+
3636
## Example
3737
38-
To fix the previous code example, make sure `i+j` can't overflow. For example:
38+
To fix the previous code example in which `i+j` might overflow, introduce a check to make sure it won't. For example:
3939
4040
```cpp
4141
void *SmallAlloc(int);
@@ -54,5 +54,5 @@ void foo(unsigned short i, unsigned short j)
5454

5555
## See also
5656

57-
[C26831](c26831.md)\
58-
[C26833](c26833.md)
57+
[`C26831`](c26831.md)\
58+
[`C26833`](c26833.md)

docs/code-quality/c26833.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ ms.date: 03/06/2023
55
f1_keywords: ["C26833", "ALLOCATION_POTENTIAL_OVERFLOW_BEFORE_CHECK"]
66
helpviewer_keywords: ["C26833"]
77
---
8-
# Warning C26833
8+
# Warning `C26833`
99

1010
> Allocation size might be the result of a numerical overflow before the bound check
1111
1212
## Remarks
1313

14-
Checks for an allocation whose size may be the result of a numerical overflow. This check is targeted to find the following code pattern:
14+
This waraning reports that the size specified for an allocation may be the result of a numerical overflow. For example:
1515

1616
```cpp
1717
void* SmallAlloc(int);
@@ -21,20 +21,23 @@ void foo(unsigned i, unsigned j)
2121
unsigned size = i + j;
2222

2323
if (size > 50)
24+
{
2425
return;
26+
}
2527

2628
int* p = (int*)SmallAlloc(size + 5); // Warning: C26833
2729
p[j] = 5;
2830
}
2931
```
3032
31-
The code example above has a check `size > 50`. Unfortunately, this check is too late. In case `i + j` overflows, it produces a small value that passes the check. So, `SmallAlloc` allocates a buffer smaller than expected. Future accesses of the buffer like `p[i]` are out of bounds. These code patterns can result in remote code execution vulnerabilities.
32-
Common allocation functions like `new`, `malloc`, and `VirtualAlloc` are recognized. The check also tries to recognize custom allocator functions by case insensitive search for the `alloc` substring in the function name.
33+
The check for `size > 50` is too late. If `i + j` overflows, it produces a small value that will pass the check. Then, `SmallAlloc` will allocate 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.
3334
34-
Our analysis engine's numerical solver have some limitations reasoning about numerical overflows. As a result, this check uses some heuristics and sometimes fails to recognize that certain checks can prevent overflows.
35-
Usually, checking for a reasonable bound on the allocation should work.
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.
3638
3739
This warning is available in Visual Studio 2022 version 17.7 and later versions.
40+
3841
## Example
3942
4043
To fix the previous code example, make sure `i+j` cannot overflow. For example:
@@ -45,12 +48,16 @@ void* SmallAlloc(int);
4548
void foo(unsigned i, unsigned j)
4649
{
4750
if (i > 100 || j > 100)
51+
{
4852
return;
53+
}
4954
5055
unsigned size = i + j;
5156
5257
if (size > 50)
58+
{
5359
return;
60+
}
5461
5562
int* p = (int*)SmallAlloc(size + 5);
5663
p[j] = 5;
@@ -59,5 +66,5 @@ void foo(unsigned i, unsigned j)
5966

6067
## See also
6168

62-
[C26831](c26831.md)\
63-
[C26832](c26832.md)
69+
[`C26831`](c26831.md)\
70+
[`C26832`](c26832.md)

docs/code-quality/c26835.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
---
22
title: Warning C26835
33
description: "Describes the Microsoft C/C++ code analysis warning C26835, its causes, and how to address it."
4-
ms.date: 03/06/2023
4+
ms.date: 03/20/2023
55
f1_keywords: ["C26835", "RTL_COMPARE_MEMORY_MISUSE"]
66
helpviewer_keywords: ["C26835"]
77
---
8-
# Warning C26835
8+
# Warning `C26835`
99

1010
> `RtlCompareMemory` returns the number of matching bytes. Consider replacing this call with `RtlEqualMemory`
1111
1212
## Remarks
1313

14-
When `RtlCompareMemory`'s return value is used in a boolean context, it evaluates to true when there is at least 1 byte that is equal before finding a difference. This behavior is often not intentional. Moreover, comparing the result of `RtlCompareMemory` to 0 evaluates to false if there is at least 1 matching byte. This behavior is different from functions with compare in their name like `strcmp` making the code harder to understand. To check for equality, consider using `RtlEqualMemory` instead.
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 is different from other comparison functions such as `strcmp`, making the code harder to understand. To check for equality, consider using `RtlEqualMemory` instead.
1515

1616
This warning is available in Visual Studio 2022 version 17.7 and later versions.
1717

18-
## Examples
18+
## Example
1919

2020
```cpp
2121
int foo(const void* ptr)
2222
{
23-
if (RtlCompareMemory("test", ptr, 5) == 0) // C26835
24-
{
25-
// ...
26-
}
23+
if (RtlCompareMemory("test", ptr, 5) == 0) // C26835
24+
{
25+
// ...
26+
}
2727
}
2828
```
2929
30-
To fix the issue, verify if the original intention was to check for equality and replace the function call with `RtlEqualMemory`.
30+
To fix the issue, verify if the original intention was to check for equality and replace the function call with `RtlEqualMemory`:
3131
3232
```cpp
3333
int foo(const void* ptr)
3434
{
35-
if (RtlEqualMemory("test", ptr, 5)) // C26835
36-
{
37-
// ...
38-
}
35+
if (RtlEqualMemory("test", ptr, 5)) // C26835
36+
{
37+
// ...
38+
}
3939
}
4040
```
4141

4242
## See also
4343

44-
[RtlEqualMemory macro (wdm.h)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlequalmemory.md)\
45-
[RtlCompareMemory function (wdm.h)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlequalmemory.md)
44+
[`RtlEqualMemory` macro (`wdm.h`)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlequalmemory.md)\
45+
[`RtlCompareMemory` function (`wdm.h`)](/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlequalmemory.md)

0 commit comments

Comments
 (0)