C and C++


BADMACRO.STDLIB_H_MEM : stdlib.hで宣言されたAllocator Macro及びDeallocator Macroの使用

要旨

<stdlib.h> (C) / <cstdlib> (C++)で宣言されマクロとして定義された以下を使用しています: malloc()calloc()realloc()free()

一部のコーディング規約では、これらのマクロは認められていません。

libcの実装でこれらを 関数として定義している場合、このワーニングの代わりにUse of <stdlib.h> Allocator/Deallocatorが検出されます。

プロパティ

クラス名 Use of <stdlib.h> Allocator/Deallocator Macro
日本語クラス名 stdlib.hで宣言されたAllocator Macro及びDeallocator Macroの使用
クラス分類 スタイル (style)
ニーモニック BADMACRO.STDLIB_H_MEM
カテゴリー
MisraC2023 MisraC2023:21.3 The memory allocation and deallocation functions of <stdlib.h> shall not be used
Misra2012 Misra2012:21.3 The memory allocation and deallocation functions of <stdlib.h> 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.
MisraC++2023 MisraC++2023:21.6.1 Dynamic memory shall not be used
  MisraC++2023:21.6.2 Dynamic memory shall be managed automatically
CWE CWE:710 Improper Adherence to Coding Standards
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="Use of <stdlib.h> Allocator/Deallocator Macro"

#include <cstdlib>

class Intpair {
  public:
    int a;
    int b;
    Intpair(int aval, int bval): a(aval),b(bval){}
    int sum(){return a+b;}
};

int baduse(){
    int s;
    Intpair *i = (Intpair*)malloc(sizeof(Intpair));   // Warning issued here:
                      // - 'Use of <stdlib.h> Allocator/Deallocator Macro' if malloc() implemented as a macro
                      // - 'Use of <stdlib.h> Allocator/Deallocator' if malloc() implemented as a function 
    if (i==NULL) {return 0;}
    i->a = 5;
    i->b = 7;
    s = i->sum();
    free(i);          // Warning issued here:
                      // - 'Use of <stdlib.h> Allocator/Deallocator Macro' if free() implemented as a macro
                      // - 'Use of <stdlib.h> Allocator/Deallocator' if free() implemented as a function 
    return s;
}

int gooduse(){                              // OK: uses constructor and destructor instead of malloc/free. 
    Intpair *i = new Intpair(5,7);                   
    int s =  i->sum();
    delete i;
    return s;

}

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

このクラスは一般テンプレート設定ファイルで BAD_MACRO_* ルールセットによって実装されています。

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