CWE Rule 590
Description
Rule Description
The product calls free() on a pointer to memory that was not allocated using associated heap allocation functions such as malloc(), calloc(), or realloc().
Polyspace Implementation
The rule checker checks for Invalid free of pointer.
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); }
Check Information
Category: Others |
Version History
Introduced in R2024a
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 (한국어)