JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


ALLOC.POSTINIT : Dynamic Allocation After Initialization

Summary

A program performs dynamic allocation outside the initialization phase.

For the purpose of this check, memory allocation is considered to be in the initialization phase if it takes place in a function f() specified with configuration file parameter DYN_INIT_FUNCTIONS, or in a function directly or transitively called by f().

Properties

Class Name Dynamic Allocation After Initialization
Significance style
Mnemonic ALLOC.POSTINIT
Categories
MisraC2025 MisraC2025:21.3 The memory allocation and deallocation functions of <stdlib.h> shall not be used
  MisraC2025:D.4.12 Dynamic memory allocation shall not be used
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-7 If non-realtime implementation of dynamic memory management functions is used in the project, then memory shall only be allocated and deallocated during non-realtime program phases.
MisraC++2008 MisraC++2008:18-4-1 Dynamic heap memory allocation shall not be used.
MisraC++2023 MisraC++2023:21.6.2 Dynamic memory shall be managed automatically
CWE CWE:710 Improper Adherence to Coding Standards
JSF++ JSF++:206 Allocation/deallocation from/to the free store (heap) shall not occur after initialization.
POW10 POW10:3 Do not use dynamic memory allocation after initialization.
JPL JPL:5 Do not use dynamic memory allocation after task initialization.
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="Dynamic Allocation After Initialization"

Example

#include <stdlib.h>

char * disk_buff;
char * network_buff;

/* With factory settings, function name 'dyn_init' matches a
 * DYN_INIT_FUNCTIONS rule, so this function can directly or
 * transitively call allocator functions without triggering a 'Dynamic
 * Allocation After Initialization' warning
 */
void dyn_init(void){
    disk_buff = malloc(1024);
    network_buff = malloc(2048);
}

int main(int argc, char *argv[]){
    char *r;
    char rv = 0;
    /* starting process - do all dynamic allocation now */
    dyn_init();

    /* no more dynamic allocation from this point on */
    /* ... */
    r = malloc(5);      /* 'Dynamic Allocation After Initialization' warning issued here */
    /* ... */
    if (r){
      rv = 1;
      free(r);
    }
    return rv;
}

Triggering Functions

Warnings of this class can be triggered by the following functions.

Notes

Only code reachable from a program entry point can trigger warnings for this class. CodeSonar will treat a function as a program entry point if it is specified with configuration file parameter PROGRAM_ENTRY_POINTS. The factory setting of this parameter instructs CodeSonar to treat main() and init() as entry points.

Relevant Configuration File Parameters

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

 

To report problems with this documentation, please visit https://support.codesecure.com/.