C and C++


ALLOC.POSTINIT : 初期化後の動的な割り当て

要旨

初期化フェーズ以外で動的なメモリ割り当てを行いました。

このチェックの目的は、関数内の初期化フェーズで直接もしくは DYN_INIT_FUNCTIONS パラメータで設定された関数 f() から間接的にメモリ割り当てが行われていることを確認することです。

プロパティ

クラス名 Dynamic Allocation After Initialization
日本語クラス名 初期化後の動的な割り当て
クラス分類 スタイル (style)
ニーモニック ALLOC.POSTINIT
カテゴリー
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.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Dynamic Allocation After Initialization"

#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(){
    disk_buff = malloc(1024);
    network_buff = malloc(2048);
}

int main(){
    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;
}

ワーニングを引き起こす関数

このクラスを引き起こす関数は以下となります。

注釈

プログラムエントリーポイントから到達可能なコードのみこのクラスのワーニングを引き起こすことが可能です。 CodeSonar は設定ファイルの PROGRAM_ENTRY_POINTS パラメータに設定されている関数をプログラムエントリーポイントとみなします。 このパラメータは main()init() がデフォルトで設定されています。

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。