CWE Rule 762
Description
Rule Description
The application attempts to return a memory resource to the system, but it calls a release function that is not compatible with the function that was originally used to allocate that resource.
Polyspace Implementation
The rule checker checks for these issues:
Invalid free of pointer
Mismatched alloc/dealloc functions on Windows
Examples
Invalid free of pointer
This issue occurs
when a block of memory released using the free
function
was not previously allocated using malloc
, calloc
,
or realloc
.
The free
function releases a block of memory allocated on the
heap. If you try to access a location on the heap that you did not allocate
previously, a segmentation fault can occur.
The issue can highlight coding errors. For instance, you perhaps wanted to use the
free
function or a previous malloc
function on a different pointer.
In most cases, you can fix the issue by removing the free
statement. If the pointer is not allocated memory from the heap with
malloc
or calloc
, you do not need to free
the pointer. You can simply reuse the pointer as required.
If the issue highlights a coding error such as use of free
or
malloc
on the wrong pointer, correct the error.
If the issue occurs because you use the free
function to free
memory allocated with the new
operator, replace the
free
function with the delete
operator.
#include <stdlib.h> void Assign_Ones(void) { int p[10]; for(int i=0;i<10;i++) *(p+i)=1; free(p); //Noncompliant /* Defect: p does not point to dynamically allocated memory */ }
The pointer p
is deallocated
using the free
function. However, p
points
to a memory location that was not dynamically allocated.
If the number of elements of the array p
is
known at compile time, one possible correction is to remove the deallocation
of the pointer p
.
#include <stdlib.h> void Assign_Ones(void) { int p[10]; for(int i=0;i<10;i++) *(p+i)=1; /* Fix: Remove deallocation of p */ }
If the number of elements of the array p
is
not known at compile time, one possible correction is to dynamically
allocate memory to the array p
.
#include <stdlib.h> void Assign_Ones(int num) { int *p; /* Fix: Allocate memory dynamically to p */ p=(int*) calloc(10,sizeof(int)); for(int i=0;i<10;i++) *(p+i)=1; free(p); }
Mismatched alloc/dealloc functions on Windows
This issue occurs when you use a Windows® deallocation function that is not properly paired to its corresponding allocation function.
Deallocating memory with a function that does not match the allocation function can cause memory corruption or undefined behavior. If you are using an older version of Windows, the improper function can also cause compatibility issues with newer versions.
Properly pair your allocation and deallocation functions according to the functions listed in this table.
Allocation Function | Deallocation Function |
---|---|
malloc() | free() |
realloc() | free() |
calloc() | free() |
_aligned_malloc() | _aligned_free() |
_aligned_offset_malloc() | _aligned_free() |
_aligned_realloc() | _aligned_free() |
_aligned_offset_realloc() | _aligned_free() |
_aligned_recalloc() | _aligned_free() |
_aligned_offset_recalloc() | _aligned_free() |
_malloca() | _freea() |
LocalAlloc() | LocalFree() |
LocalReAlloc() | LocalFree() |
GlobalAlloc() | GlobalFree() |
GlobalReAlloc() | GlobalFree() |
VirtualAlloc() | VirtualFree() |
VirtualAllocEx() | VirtualFreeEx() |
VirtualAllocExNuma() | VirtualFreeEx() |
HeapAlloc() | HeapFree() |
HeapReAlloc() | HeapFree() |
#ifdef _WIN32_ #include <windows.h> #else #define _WIN32_ typedef void *HANDLE; typedef HANDLE HGLOBAL; typedef HANDLE HLOCAL; typedef unsigned int UINT; extern HLOCAL LocalAlloc(UINT uFlags, UINT uBytes); extern HLOCAL LocalFree(HLOCAL hMem); extern HGLOBAL GlobalFree(HGLOBAL hMem); #endif #define SIZE9 9 void func(void) { /* Memory allocation */ HLOCAL p = LocalAlloc(0x0000, SIZE9); if (p) { /* Memory deallocation. */ GlobalFree(p); //Noncompliant } }
In this example, memory is allocated with LocallAlloc()
. The program
then erroneously uses GlobalFree()
to deallocate the memory.
When you allocate memory with LocalAllocate()
, use
LocalFree()
to deallocate the memory.
#ifdef _WIN32_ #include <windows.h> #else #define _WIN32_ typedef void *HANDLE; typedef HANDLE HGLOBAL; typedef HANDLE HLOCAL; typedef unsigned int UINT; extern HLOCAL LocalAlloc(UINT uFlags, UINT uBytes); extern HLOCAL LocalFree(HLOCAL hMem); extern HGLOBAL GlobalFree(HGLOBAL hMem); #endif #define SIZE9 9 void func(void) { /* Memory allocation */ HLOCAL p = LocalAlloc(0x0000, SIZE9); if (p) { /* Memory deallocation. */ LocalFree(p); } }
Check Information
Category: Others |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)