Skip to content

Commit 6e27d70

Browse files
committed
edits
1 parent 75b7f06 commit 6e27d70

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

docs/code-quality/c26838.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
---
2-
# Required metadata
3-
# For more information, see https://review.learn.microsoft.com/en-us/help/platform/learn-editor-add-metadata?branch=main
4-
# For valid values of ms.service, ms.prod, and ms.topic, see https://review.learn.microsoft.com/en-us/help/platform/metadata-taxonomies?branch=main
5-
62
title: Warning C26838
73
description: Learn about Microsoft C++ code analysis warning C26838.
84
author: Rastaban
@@ -12,35 +8,35 @@ ms.date: 1/10/2025
128
---
139
# Warning C26838
1410

15-
> Allocation size is the result of a signed to unsigned narrowing conversion that could result in overflow if the signed value is negative
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.
1614

1715
## Remarks
1816

19-
This warning 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:
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:
2018

2119
```cpp
2220
void* CustomAlloc(size_t);
2321

2422
int* CreateIntArray(int numberOfElements)
2523
{
2624
int* p = (int*)CustomAlloc(numberOfElements * sizeof(int)); // Warning: C26838
27-
// ...
25+
2826
return p;
2927
}
3028
```
3129
32-
In 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
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
3331
by `sizeof(int)`. When `numberOfElements` is negative, the resulting value may overflow or have unexpected results when passed to `CustomAlloc`.
3432
3533
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.
3634
3735
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
3836
39-
This warning is available in Visual Studio 2022 version 17.13 and later versions.
40-
4137
## Example
4238
43-
To fix the previous code example in which `numberOfElements * sizeof(int)` might overflow due to a negative signed value, introduce a check to make sure it won't. For example:
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:
4440
4541
```cpp
4642
void* CustomAlloc(size_t);
@@ -56,13 +52,11 @@ int* CreateIntArray(int numberOfElements)
5652
}
5753
```
5854

59-
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).
60-
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.
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.
6156

6257
## See also
6358

6459
[`C26831`](c26831.md)\
6560
[`C26832`](c26832.md)\
6661
[`C26833`](c26833.md)\
67-
[`C26833`](c26839.md)
68-
62+
[`C26833`](c26839.md)

docs/code-quality/c26839.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
---
2-
# Required metadata
3-
# For more information, see https://review.learn.microsoft.com/en-us/help/platform/learn-editor-add-metadata?branch=main
4-
# For valid values of ms.service, ms.prod, and ms.topic, see https://review.learn.microsoft.com/en-us/help/platform/metadata-taxonomies?branch=main
5-
62
title: Warning C26839
73
description: Learn about Microsoft C++ code analysis warning C26839.
84
author: Rastaban
@@ -12,11 +8,13 @@ ms.date: 1/10/2025
128
---
139
# Warning C26839
1410

15-
> 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
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.
1614

1715
## Remarks
1816

19-
This warning 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:
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:
2018

2119
```cpp
2220
int* CreateIntArray(int size)
@@ -26,18 +24,15 @@ int* CreateIntArray(int size)
2624
}
2725
```
2826
29-
In the expression `new int[size]`, `size` is signed. The compiler converts the signed value to an unsigned value to calculate how many bytes need to be allocated for the array.
30-
When `size` is negative, the result of that calculation may overflow or have unexpected results.
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 allocate for the array. When `size` is negative, the result of that calculation may overflow or have unexpected results when passed to `new`.
3128
32-
This check is the same as [`C26838`](c26838.md), but applies only to array new `new T[]`.
29+
This check is the same as [`C26838`](c26838.md), but applies only to `new T[]`.
3330
3431
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
3532
36-
This warning is available in Visual Studio 2022 version 17.12 and later versions.
37-
3833
## Example
3934
40-
To fix the previous code example in which the size calculation might overflow due to a negative signed value, introduce a check to make sure it won't. For example:
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:
4136
4237
```cpp
4338
int* CreateIntArray(int size)

docs/code-quality/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ items:
633633
href: ../code-quality/c26830.md
634634
- name: Warning C26831
635635
href: ../code-quality/c26831.md
636-
- name: " Warning C26838"
636+
- name: Warning C26838
637637
href: c26838.md
638638
- name: Warning C26839
639639
href: c26839.md

0 commit comments

Comments
 (0)