Main Content

CERT C: Rule MEM34-C

Only free memory allocated dynamically

Description

Rule Definition

Only free memory allocated dynamically.1

Polyspace Implementation

The rule checker checks for these issues:

  • Invalid free of pointer

  • Invalid reallocation of pointer

Examples

expand all

Issue

Invalid free of pointer occurs when a block of memory released using the free function was not previously allocated using malloc, calloc, or realloc.

Risk

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 on a different pointer.

Fix

In most cases, you can fix the issue by removing the free statement. If a 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.

Example - Invalid Free of Pointer Error
#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.

Correction — Remove Pointer Deallocation

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 */
 }
Correction — Introduce Pointer Allocation

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); 
}
Issue

Invalid reallocation of pointer occurs when a block of memory reallocated using the realloc function was not previously allocated using malloc or calloc.

Risk

Reallocating a block of memory that was not allocated dynamically allocated can result in undefined behavior.

The issue can highlight coding errors. For instance, you perhaps wanted to use the realloc function on a different pointer.

Fix

If you want to reallocate a block of memory, make sure that it was dynamically allocated in the first place.

Example – Reallocation of Memory Allocated Statically
#include <stdlib.h>
  
#define SIZE 256

void reshape(int isSpaceAvailable) {
  char buf[SIZE];
  char *newBuf;
  newBuf = (char *)realloc(buf, 2 * SIZE); //Noncompliant
  
  if (newBuf == NULL) {
    /* Handle error */
  }
}

In this example, the buffer buf is not allocated dynamically. Therefore, the reallocation of buf results in undefined behavior.

Example – Reallocation of Memory Allocated Statically

Make sure that a buffer that you reallocate was previous allocated memory dynamically.

#include <stdlib.h>
  
#define SIZE 256

void reshape(void){
  char *buf;
  char *newBuf;
  buf = (char*)malloc(SIZE *sizeof(char));
  newBuf = (char *)realloc(buf, 2 * SIZE);
  
  if (newBuf == NULL) {
    /* Handle error */
  }
  free(newBuf);
}

Check Information

Group: Rule 08. Memory Management (MEM)

Version History

Introduced in R2019a

expand all


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.