Skip to content

Commit b9ad53a

Browse files
author
Jill Grant
authored
Merge pull request #5651 from Rastaban/docs-editor/c26831-1724366717
Add c26838 and c26839 documentation
2 parents b3f5fcb + 0818d9b commit b9ad53a

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

docs/code-quality/c26831.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ void foo(int i, int j)
5858
## See also
5959

6060
[`C26832`](c26832.md)\
61-
[`C26833`](c26833.md)
61+
[`C26833`](c26833.md)\
62+
[`C26838`](c26838.md)\
63+
[`C26839`](c26839.md)

docs/code-quality/c26838.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Warning C26838
3+
description: Learn about Microsoft C++ code analysis warning C26838.
4+
author: Rastaban
5+
ms.author: philc
6+
ms.topic: reference
7+
ms.date: 1/10/2025
8+
---
9+
# Warning C26838
10+
11+
> Allocation size is the result of a signed to unsigned narrowing conversion that could result in overflow if the signed value is negative.
12+
13+
This warning was added in Visual Studio 2022 version 17.13.
14+
15+
## Remarks
16+
17+
Reports that the size specified for an allocation may be the result of the conversion of a possibly negative signed value to an unsigned value. For example:
18+
19+
```cpp
20+
void* CustomAlloc(size_t);
21+
22+
int* CreateIntArray(int numberOfElements)
23+
{
24+
int* p = (int*)CustomAlloc(numberOfElements * sizeof(int)); // Warning: C26838
25+
26+
return p;
27+
}
28+
```
29+
30+
The expression `numberOfElements * sizeof(int)`, `numberOfElements` is signed and `sizeof(int)` is unsigned. On 64-bit machines, `numberOfElements` is promoted to an unsigned value when multiplied
31+
by `sizeof(int)`. When `numberOfElements` is negative, the resulting value may overflow or have unexpected results when passed to `CustomAlloc`.
32+
33+
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.
34+
35+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
36+
37+
## Example
38+
39+
To fix the previous code example in which `numberOfElements * sizeof(int)` might overflow due to a negative signed value, introduce a check to ensure it won't. For example:
40+
41+
```cpp
42+
void* CustomAlloc(size_t);
43+
44+
int* CreateIntArray(int numberOfElements)
45+
{
46+
if (numberOfElements < 0)
47+
return nullptr;
48+
49+
int* p = (int*)CustomAlloc(numberOfElements * sizeof(int));
50+
// ...
51+
return p;
52+
}
53+
```
54+
55+
In the previous example, checking for a negative value addresses the `C26832` warning. Depending on the size of the types involved, this check may result in a different warning such as [`C26831`](c26831.md). For example, on a 32-bit system, both `int` and `size_t` are 32 bits, so the result of the multiplication can still overflow without negative values.
56+
57+
## See also
58+
59+
[`C26831`](c26831.md)\
60+
[`C26832`](c26832.md)\
61+
[`C26833`](c26833.md)\
62+
[`C26833`](c26839.md)

docs/code-quality/c26839.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Warning C26839
3+
description: Learn about Microsoft C++ code analysis warning C26839.
4+
author: Rastaban
5+
ms.author: philc
6+
ms.topic: reference
7+
ms.date: 1/10/2025
8+
---
9+
# Warning C26839
10+
11+
> Array new allocation size is the result of a signed to unsigned narrowing conversion that could result in overflow if the signed value is negative.
12+
13+
This warning was added in Visual Studio 2022 version 17.13.
14+
15+
## Remarks
16+
17+
Reports that the size specified for an array `new` allocation may be the result of the conversion of a possibly negative signed value to an unsigned value. For example:
18+
19+
```cpp
20+
int* CreateIntArray(int size)
21+
{
22+
int* intArray = new int[size];
23+
return intArray;
24+
}
25+
```
26+
27+
The expression `new int[size]`, `size` is signed. The compiler converts the signed value to an unsigned value to calculate how many bytes to be allocated for the array. When `size` is negative, the result of that calculation may overflow or have unexpected results when passed to `new`.
28+
29+
This check is the same as [`C26838`](c26838.md), but applies only to `new T[]`.
30+
31+
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
32+
33+
## Example
34+
35+
To fix the previous code example in which the size calculation might overflow due to a negative signed value, introduce a check to ensure it won't. For example:
36+
37+
```cpp
38+
int* CreateIntArray(int size)
39+
{
40+
if (size < 0)
41+
return nullptr;
42+
43+
int* intArray = new int[size];
44+
return intArray;
45+
}
46+
```
47+
48+
## See also
49+
50+
[`C26831`](c26831.md)\
51+
[`C26832`](c26832.md)\
52+
[`C26838`](c26833.md)\
53+
[`C26838`](c26838.md)

docs/code-quality/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ items:
633633
href: ../code-quality/c26830.md
634634
- name: Warning C26831
635635
href: ../code-quality/c26831.md
636+
- name: Warning C26838
637+
href: c26838.md
638+
- name: Warning C26839
639+
href: c26839.md
636640
- name: Warning C26832
637641
href: ../code-quality/c26832.md
638642
- name: Warning C26833

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy