ISO/IEC TS 17961 [accfree]
Accessing freed memory
Description
Rule Definition
Accessing freed memory.1
Polyspace Implementation
This checker checks for these issues:
Use of previously freed pointer.
Invalid use of standard library string routine.
Examples
Use of previously freed pointer
Use of previously freed pointer occurs when you access a
block of memory after freeing the block using the free
function.
When a pointer is allocated dynamic memory with malloc
,
calloc
or realloc
, it points to a memory
location on the heap. When you use the free
function on this
pointer, the associated block of memory is freed for reallocation. Trying to access
this block of memory can result in unpredictable behavior or even a segmentation
fault.
The fix depends on the root cause of the defect. See if you intended to free the memory later or allocate another memory block to the pointer before access.
As a good practice, after you free a memory block, assign the corresponding pointer to NULL. Before dereferencing pointers, check them for NULL values and handle the error. In this way, you are protected against accessing a freed block.
#include <stdlib.h>
#include <stdio.h>
int increment_content_of_address(int base_val, int shift)
{
int j;
int* pi = (int*)malloc(sizeof(int));
if (pi == NULL) return 0;
*pi = base_val;
free(pi);
j = *pi + shift;
/* Defect: Reading a freed pointer */
return j;
}
The free
statement releases
the block of memory that pi
refers to. Therefore,
dereferencingpi
after the free
statement
is not valid.
One possible correction is to free the pointer pi
only
after the last instance where it is accessed.
#include <stdlib.h> int increment_content_of_address(int base_val, int shift) { int j; int* pi = (int*)malloc(sizeof(int)); if (pi == NULL) return 0; *pi = base_val; j = *pi + shift; *pi = 0; /* Fix: The pointer is freed after its last use */ free(pi); return j; }
Invalid use of standard library string routine
Invalid use of standard library string routine occurs when a string library function is called with invalid arguments.
The risk depends on the type of invalid arguments. For instance, using the
strcpy
function with a source argument larger than the
destination argument can result in buffer overflows.
The fix depends on the standard library
function involved in the defect. In some cases, you can constrain the function arguments
before the function call. For instance, if the strcpy
function:
char * strcpy(char * destination, const char* source);
strcpy
. In
some cases, you can use an alternative function to avoid the error. For instance, instead
of strcpy
, you can use strncpy
to control the number
of bytes copied. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
#include <string.h>
#include <stdio.h>
char* Copy_String(void)
{
char *res;
char gbuffer[5],text[20]="ABCDEFGHIJKL";
res=strcpy(gbuffer,text);
/* Error: Size of text is less than gbuffer */
return(res);
}
The string text
is larger
in size than gbuffer
. Therefore, the function strcpy
cannot
copy text
into gbuffer
.
One possible correction is to declare the destination
string gbuffer
with equal or larger size than the
source string text
.
#include <string.h> #include <stdio.h> char* Copy_String(void) { char *res; /*Fix: gbuffer has equal or larger size than text */ char gbuffer[20],text[20]="ABCDEFGHIJKL"; res=strcpy(gbuffer,text); return(res); }
Check Information
Decidability: Undecidable |
Version History
Introduced in R2019a
1 Extracts from the standard "ISO/IEC TS 17961 Technical Specification - 2013-11-15" are reproduced with the agreement of AFNOR. Only the original and complete text of the standard, as published by AFNOR Editions - accessible via the website www.boutique.afnor.org - has normative value.
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 (한국어)