Skip to content

Commit 6d99907

Browse files
Updated C6308
Changed wording to be more concise and less hostile. Changed the format and wording to match my other PRs
1 parent 64857d3 commit 6d99907

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

docs/code-quality/c6308.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ ms.assetid: 1162cd96-9037-4576-9858-0c8361a12559
99
---
1010
# 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+
**Warning C6308: Memory Reallocation Leak (REALLOCLEAK)**\
13+
Example output:
14+
> '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
1315
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.
16+
## Description
17+
18+
This warning indicates a memory leak due to the unsafe use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the issue, assign the result of the reallocation function to a temporary location, and then replace the original pointer after successful reallocation.
1519

1620
## Example
1721

18-
The following sample code generates this warning:
22+
The following sample code generates this warning. This issue stems from the assignment of x to the return value from realloc. If realloc fails and returns a null pointer, then x will fail to be freed:
1923

2024
```cpp
2125
#include <malloc.h>
@@ -34,7 +38,7 @@ void f( )
3438
}
3539
```
3640
37-
To correct this warning, use the following code:
41+
To resolve the issue, you can create a temporary variable to store the return status of realloc. This allows you to free the previously allocated memory safely in the case of realloc failing:
3842
3943
```cpp
4044
#include <malloc.h>
@@ -43,9 +47,7 @@ To correct this warning, use the following code:
4347
void f()
4448
{
4549
char *x, *tmp;
46-
4750
x = (char *) malloc(10);
48-
4951
if (x != NULL)
5052
{
5153
tmp = (char *) realloc(x,512);
@@ -61,7 +63,7 @@ void f()
6163

6264
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.
6365

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).
66+
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).
6567

6668
## See also
6769

0 commit comments

Comments
 (0)