You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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
-
6
2
title: Warning C26838
7
3
description: Learn about Microsoft C++ code analysis warning C26838.
8
4
author: Rastaban
@@ -12,35 +8,35 @@ ms.date: 1/10/2025
12
8
---
13
9
# Warning C26838
14
10
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.
16
14
17
15
## Remarks
18
16
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:
20
18
21
19
```cpp
22
20
void* CustomAlloc(size_t);
23
21
24
22
int* CreateIntArray(int numberOfElements)
25
23
{
26
24
int* p = (int*)CustomAlloc(numberOfElements * sizeof(int)); // Warning: C26838
27
-
// ...
25
+
28
26
return p;
29
27
}
30
28
```
31
29
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
33
31
by `sizeof(int)`. When `numberOfElements` is negative, the resulting value may overflow or have unexpected results when passed to `CustomAlloc`.
34
32
35
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.
36
34
37
35
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
38
36
39
-
This warning is available in Visual Studio 2022 version 17.13 and later versions.
40
-
41
37
## Example
42
38
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:
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.
Copy file name to clipboardExpand all lines: docs/code-quality/c26839.md
+7-12Lines changed: 7 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,4 @@
1
1
---
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
-
6
2
title: Warning C26839
7
3
description: Learn about Microsoft C++ code analysis warning C26839.
8
4
author: Rastaban
@@ -12,11 +8,13 @@ ms.date: 1/10/2025
12
8
---
13
9
# Warning C26839
14
10
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.
16
14
17
15
## Remarks
18
16
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:
20
18
21
19
```cpp
22
20
int* CreateIntArray(int size)
@@ -26,18 +24,15 @@ int* CreateIntArray(int size)
26
24
}
27
25
```
28
26
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`.
31
28
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[]`.
33
30
34
31
This check sometimes fails to recognize that certain checks can prevent overflows because the check is conservative.
35
32
36
-
This warning is available in Visual Studio 2022 version 17.12 and later versions.
37
-
38
33
## Example
39
34
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:
0 commit comments