C and C++ Binaries


BADFUNC.REALLOC : Use of realloc

Summary

A use of realloc(), which is not guaranteed to preserve alignment in all cases.

Properties

Class Name Use of realloc
Significance security
Mnemonic BADFUNC.REALLOC
Categories
MisraC2023 MisraC2023:21.3 The memory allocation and deallocation functions of <stdlib.h> shall not be used
  MisraC2023:D.4.12 Dynamic memory allocation shall not be used
Misra2012 Misra2012:21.3 The memory allocation and deallocation functions of <stdlib.h> shall not be used
  Misra2012:D.4.12 Dynamic memory allocation shall not be used
Misra2004 Misra2004:20.4 Dynamic heap memory allocation shall not be used
AUTOSARC++14 AUTOSARC++14:A18-5-1 Functions malloc, calloc, realloc and free shall not be used.
MisraC++2008 MisraC++2008:18-4-1 Dynamic heap memory allocation shall not be used.
CWE CWE:676 Use of Potentially Dangerous Function
CERT-C CERT-C:MEM36-C Do not modify the alignment of objects by calling realloc()
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Use of realloc"

Example

#include <stdlib.h>

typedef struct my_128b_aligned_type {
    int i;
    char c;
    /*  ... */
} my_aligned_t;                                                           /* user-defined type that must be aligned to a 128-byte boundary */

my_aligned_t * reallocate_realloc(my_aligned_t *obj){
    return realloc(obj, sizeof(my_aligned_t));        /* 'Use of realloc' warning issued here:
                                                       * realloc() does not preserve the alignment properties required for my_aligned_type
                                                       */ 
}

my_aligned_t * reallocate_posix_memalign(my_aligned_t *obj){
  void *rv = obj;
  if (posix_memalign(&rv, 128, sizeof(my_aligned_t))){                    /* ok: posix_memalign() lets us specify the required alignment */
    return NULL;
  }
  if (obj){
    free(obj);                                                            /* must explicitly free previous object to avoid memory leaks< */
  }
  return rv;
}

Relevant Configuration File Parameters

This class is implemented using a BAD_FUNCTION_* rule set in the general template configuration file.

The following configuration file parameters affect checks for this warning class.