|
1 | 1 | ---
|
2 |
| -title: C6308 |
| 2 | +title: Warning C6308 |
3 | 3 | 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 |
5 | 5 | ms.topic: reference
|
6 |
| -f1_keywords: ["C6308"] |
| 6 | +f1_keywords: ["C6308", "REALLOCLEAK", "__WARNING_REALLOCLEAK"] |
7 | 7 | helpviewer_keywords: ["C6308"]
|
8 | 8 | ms.assetid: 1162cd96-9037-4576-9858-0c8361a12559
|
9 | 9 | ---
|
10 |
| -# C6308 |
| 10 | +# Warning C6308 |
11 | 11 |
|
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 |
13 | 13 |
|
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 |
15 | 19 |
|
16 | 20 | ## Example
|
17 | 21 |
|
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: |
19 | 23 |
|
20 | 24 | ```cpp
|
21 | 25 | #include <malloc.h>
|
22 | 26 | #include <windows.h>
|
23 | 27 |
|
24 | 28 | void f( )
|
25 | 29 | {
|
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 | + } |
34 | 37 | }
|
35 | 38 | ```
|
36 | 39 |
|
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: |
38 | 41 |
|
39 | 42 | ```cpp
|
40 | 43 | #include <malloc.h>
|
41 | 44 | #include <windows.h>
|
42 | 45 |
|
43 | 46 | void f()
|
44 | 47 | {
|
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) |
53 | 50 | {
|
54 |
| - x = tmp; |
| 51 | + char *tmp = (char *) realloc(x,512); |
| 52 | + if (tmp != NULL) |
| 53 | + { |
| 54 | + x = tmp; |
| 55 | + } |
| 56 | + // code... |
| 57 | + free(x); |
55 | 58 | }
|
56 |
| - // code... |
57 |
| - free(x); |
58 |
| - } |
59 | 59 | }
|
60 | 60 | ```
|
61 | 61 |
|
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. |
63 | 63 |
|
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). |
65 | 65 |
|
66 | 66 | ## See also
|
67 | 67 |
|
68 |
| -[C6014](../code-quality/c6014.md) |
| 68 | +[Warning C6014](../code-quality/c6014.md) |
0 commit comments