Skip to content

Commit cb63fb9

Browse files
authored
Merge pull request MicrosoftDocs#4447 from MugBergerFries/patch-8
Updated C6308
2 parents 36913a4 + f3f62ce commit cb63fb9

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

docs/code-quality/c6308.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
---
2-
title: C6308
2+
title: Warning C6308
33
description: "Understand the causes of Microsoft C/C++ code analysis warning C6308, and learn how to fix them."
4-
ms.date: 10/23/2020
4+
ms.date: 09/28/2022
55
ms.topic: reference
6-
f1_keywords: ["C6308"]
6+
f1_keywords: ["C6308", "REALLOCLEAK", "__WARNING_REALLOCLEAK"]
77
helpviewer_keywords: ["C6308"]
88
ms.assetid: 1162cd96-9037-4576-9858-0c8361a12559
99
---
10-
# C6308
10+
# Warning C6308
1111

12-
> warning C6308: 'realloc' may return null pointer: assigning a null pointer to \<variable>, which is passed as an argument to 'realloc', will cause the original memory block to be leaked
12+
> 'realloc' may return null pointer: assigning a null pointer to '*parameter-name*', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
1313
14-
This warning indicates a memory leak that is the result of the incorrect use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.
14+
## Remarks
15+
16+
Heap reallocation functions don't free the passed buffer if reallocation is unsuccessful, potentially resulting in a memory leak if not handled properly. To correct the issue, assign the result of the reallocation function to a temporary variable, and then replace the original pointer after successful reallocation.
17+
18+
Code analysis name: REALLOCLEAK
1519

1620
## Example
1721

18-
The following sample code generates this warning:
22+
The following sample code generates warning C6308. This issue stems from the assignment of the return value from `realloc` to `x`. If `realloc` fails and returns a null pointer, then the original memory pointed to by `x` won't be freed:
1923

2024
```cpp
2125
#include <malloc.h>
2226
#include <windows.h>
2327

2428
void f( )
2529
{
26-
char *x;
27-
x = (char *) malloc(10);
28-
if (x != NULL)
29-
{
30-
x = (char *) realloc(x, 512);
31-
// code...
32-
free(x);
33-
}
30+
char *x = (char *) malloc(10);
31+
if (x != NULL)
32+
{
33+
x = (char *) realloc(x, 512);
34+
// code...
35+
free(x);
36+
}
3437
}
3538
```
3639
37-
To correct this warning, use the following code:
40+
To resolve the issue, you can create a temporary variable to store the return value of `realloc`. This change allows you to free the previously allocated memory safely if `realloc` fails:
3841
3942
```cpp
4043
#include <malloc.h>
4144
#include <windows.h>
4245
4346
void f()
4447
{
45-
char *x, *tmp;
46-
47-
x = (char *) malloc(10);
48-
49-
if (x != NULL)
50-
{
51-
tmp = (char *) realloc(x,512);
52-
if (tmp != NULL)
48+
char *x = (char *) malloc(10);
49+
if (x != NULL)
5350
{
54-
x = tmp;
51+
char *tmp = (char *) realloc(x,512);
52+
if (tmp != NULL)
53+
{
54+
x = tmp;
55+
}
56+
// code...
57+
free(x);
5558
}
56-
// code...
57-
free(x);
58-
}
5959
}
6060
```
6161

62-
This warning might generate noise if there is a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
62+
This warning might generate noise if there's a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.
6363

64-
To avoid these kinds of problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [shared_ptr](../standard-library/shared-ptr-class.md), [unique_ptr](../standard-library/unique-ptr-class.md), and [vector](../standard-library/vector.md). For more information, see [Smart Pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
64+
To avoid these kinds of issues altogether, you can use the mechanisms that are provided by the C++ Standard Template Library (STL). These include [`shared_ptr`](../standard-library/shared-ptr-class.md), [`unique_ptr`](../standard-library/unique-ptr-class.md), and [`vector`](../standard-library/vector.md). For more information, see [Smart pointers](../cpp/smart-pointers-modern-cpp.md) and [C++ Standard Library](../standard-library/cpp-standard-library-reference.md).
6565

6666
## See also
6767

68-
[C6014](../code-quality/c6014.md)
68+
[Warning C6014](../code-quality/c6014.md)

0 commit comments

Comments
 (0)