C and C++


LANG.CAST.PC.CONST2PTR : 整数定数からポインタへの強制型変換

要旨

整数定数表現 C は以下のいずれかに該当するとき、ポインタへ強制変換されます。

NULL の定義が整数定数表現(ポインタ型へのキャストがない)の場合、 NULL を使用している箇所でこのクラスのワーニングが検出されます。例えば:

#define NULL 0

void irreg_null(void){
    int *p = NULL;  /* 'Coercion: Integer Constant to Pointer' warning issued here  */
}

(通常の定義の #define NULL ((void *)0)が使用されているときは、この箇所ではワーニングは検出されません。)

プロパティ

クラス名 Coercion: Integer Constant to Pointer
日本語クラス名 整数定数からポインタへの強制型変換
クラス分類 スタイル (style)
ニーモニック LANG.CAST.PC.CONST2PTR
カテゴリー
MisraC2023 MisraC2023:11.9 The macro NULL shall be the only permitted form of integer null pointer constant
Misra2012 Misra2012:11.9 The macro NULL shall be the only permitted form of integer null pointer constant
AUTOSARC++14 AUTOSARC++14:M4-10-2 Literal zero (0) shall not be used as the null-pointer-constant.
MisraC++2008 MisraC++2008:4-10-2 Literal zero (0) shall not be used as the null-pointer-constant.
MisraC++2023 MisraC++2023:7.11.1 nullptr shall be the only form of the null-pointer-constant
CWE CWE:587 Assignment of a Fixed Address to a Pointer
TS17961 TS17961:5.6-argcomp 5.6. Calling functions with incorrect arguments
CERT-C CERT-C:INT31-C Ensure that integer conversions do not result in lost or misinterpreted data
  CERT-C:INT36-C Converting a pointer to integer or integer to pointer
JSF++ JSF++:182 Type casting from any type to or from pointers shall not be used.
  JSF++:183 Every possible measure should be taken to avoid type casting.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Coercion: Integer Constant to Pointer"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

#include <stdlib.h>

#define ZERO 0

void use_pointers(int *x, int *y, int *z);

int const2ptrs(int i, int *ptr){
    int y = 0;
    int *p1;
    int *p2;
    int *p3;

    if (ptr == 0){return 0;}   /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    
    p1 = NULL;                            /* macro NULL (standard definition) */
    p2 = 0;                    /* 'Coercion: Integer Constant to Pointer' warning issued here  */
    p3 = (int *) 0;                       /* explicit cast (not coercion) */
    use_pointers(p1,p2,p3);
    
    p1 = 0x080484e2;           /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    p2 = (i - y) ? ptr : 0;    /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    p3 = (i > y) ? 0 : ptr;    /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    use_pointers(p1,p2,p3);

    p1 = y;                               /* not a constant expression */
    p2 = i;                               /* not a constant expression */
    p3 = ZERO;                 /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    use_pointers(p1,p2,p3);

    
    int a = (i - y) ? ptr : 0; /* 'Coercion: Integer Constant to Pointer'  warning issued here  */
    int b = (i > y) ? 0 : ptr; /* 'Coercion: Integer Constant to Pointer'  warning issued here  */

    return a+b;
}

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

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