C and C++ Binaries


LANG.MEM.NPD : NULLポインタ参照

要旨

NULLを指すポインタを間接参照しようとしています。

デフォルトでは、ゼロページ(0から4096までのアドレス)を指すポインタを間接参照しようとすると、このワーニングを検出します。 プロジェクト設定ファイル (configuration file) の NULL_POINTER_THRESHOLD パラメータで、このしきい値を変更出来ます。

プロパティ

クラス名 Null Pointer Dereference
日本語クラス名 NULLポインタ参照
クラス分類 信頼性 (reliability)
ニーモニック LANG.MEM.NPD
カテゴリー
MisraC2023 MisraC2023:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  MisraC2023:D.4.1 Run-time failures shall be minimized
Misra2012 Misra2012:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  Misra2012:D.4.1 Run-time failures shall be minimized
AUTOSARC++14 AUTOSARC++14:A5-3-2 Null pointers shall not be dereferenced.
MisraC++2023 MisraC++2023:4.1.3 There shall be no occurrence of undefined or critical unspecified behaviour
CWE CWE:476 NULL Pointer Dereference
  CWE:573 Improper Following of Specification by Caller
  CWE:690 Unchecked Return Value to NULL Pointer Dereference
TS17961 TS17961:5.14-nullref 5.14. Dereferencing an out-of-domain pointer
CERT-C CERT-C:EXP34-C Do not dereference null pointers
CERT-CPP CERT-CPP:OOP54-CPP Gracefully handle self-copy assignment
  CERT-CPP:STR51-CPP Do not attempt to create a std::string from a null pointer
JSF++ JSF++:81 The assignment operator shall handle self-assignment correctly.
  JSF++:174 The null pointer shall not be de-referenced.
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Null Pointer Dereference"

可能性のあるカテゴリ

CWE:400 Uncontrolled Resource Consumption

#include <stddef.h>

char lang_mem_npd_simple(void){
    char *p = NULL;
    return p[0];              /* 'Null Pointer Dereference' warning issued here */
}

char lang_mem_npd_with_aliasing(int x){
    char *p1 = NULL;
    char *p2;
    char c;

    if (x < 0){
      p2 = "012345678";
    }
    else {
      p2 = p1;                 /* 'Unused Value' warning issued here
                                * - this value of p2 is only used in a statement
                                *   that causes a program crash (via Null Pointer Dereference),
                                *   so CodeSonar considers it unused.
                                */
    }
    c = p2[0];
    return c;                  /* 'Null Pointer Dereference' warning issued here */
}

char * lang_mem_npd_with_malloc(void){
    char *p = malloc(10);
    p[0] = 'X';                /* 'Null Pointer Dereference' warning issued here
                                * only when MALLOC_FAILURE_BEHAVIOR=RETURN_NULL (factory setting)
                                */
    return p;              
}

注釈

デフォルトで、 NULLポインタ参照は以下のような場合で検出されます。

char *q = malloc(10); /* malloc() can fail and return NULL */
q[0] = 'a';           /* in which case a Null Pointer Dereference occurs here */

malloc() および関連したアロケータは、失敗したとき NULL を返す可能性があります。 しかしながら、失敗する可能性が無視できるほど低い場合にはこれをチェックしたくない場合もあります。

これらのアロケータが決して失敗しないものとみなす場合には、 general configuration file templateMALLOC_FAILURE_BEHAVIOR=DOESNT_FAIL を設定してください。

MALLOC_FAILURE_BEHAVIOR の設定は全ての解析に影響することに注意してください。例えば、以下のコードでは MALLOC_FAILURE_BEHAVIOR=DOESNT_FAIL で解析された場合、 else ブロックの中のコードは到達不能とみなされます。

char *q = malloc(10); 
if (q){
   /* do something */
   }
else {
   /* do something different */
}

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

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