C and C++ Binaries


BADFUNC.REALLOC : reallocの使用

要旨

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

プロパティ

クラス名 Use of realloc
日本語クラス名 reallocの使用
クラス分類 セキュリティ (security)
ニーモニック BADFUNC.REALLOC
カテゴリー
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()
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっています。チェックを有効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += allow class="Use of realloc"

#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;
}

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

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

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