Oracle® Developer Studio 12.5: コードアナライザユーザーズガイド

印刷ビューの終了

更新: 2016 年 6 月
 
 

動的メモリーアクセスエラー

動的メモリーアクセス検査では、次の種類のエラーが検出されます。

  • ABR: 配列境界を越える読み取り (beyond array bounds read)

  • ABW: 配列境界を越える書き込み (beyond array bounds write)

  • BFM: 不正な空きメモリー (bad free memory)

  • BRP: 不正な realloc アドレスパラメータ (bad realloc address parameter)

  • CGB: 破損したガードブロック (corrupted guard block)

  • DFM: メモリーの二重解放 (double freeing memory)

  • FMR: 解放済みメモリーの読み取り (freed memory read)

  • FMW: 解放済みメモリーの書き込み (freed memory write)

  • FRP: 解放済み realloc パラメータ (freed realloc parameter)

  • IMR: 無効なメモリーの読み取り (invalid memory read)

  • IMW: 無効なメモリーの書き込み (invalid memory write)

  • MLK: メモリーリーク (memory leak)

  • OLP: 送り側と受け側の重複 (overlapping source and destination)

  • PIR: 部分的に初期化された読み取り (partially initialized read)

  • SBR: スタック境界を越える読み取り (beyond stack bounds read)

  • SBW: スタック境界を越える書き込み (beyond stack bounds write)

  • UAR: 割り当てられていないメモリーの読み取り (unallocated memory read)

  • UAW: 割り当てられていないメモリーの書き込み (unallocated memory write)

  • UMR: 初期化されていないメモリーの読み取り (uninitialized memory read)

このセクションでは、エラーの考えられる原因と、エラーが発生する可能性があるコードの例を説明します。

配列境界を越える読み取り (ABR)

考えられる原因: 配列境界を越えてメモリーを読み取ろうとしました。

例:

    int a[5];
    . . .
    printf("a[5] = %d\n",a[5]);  // Reading memory beyond array bounds

配列境界を越える書き込み (ABW)

考えられる原因: 配列境界を越えてメモリーに書き込もうとしました。

例:

    int a [5];
     . . .
     a[5] = 5; // Writing to memory beyond array bounds

不正な空きメモリー (BFM)

考えられる原因: free()() または realloc()() にヒープデータ以外のポインタを渡しました。

例:

    #include <stdlib.h>
    int main()
    {
     int *p = (int*) malloc(sizeof(int));
     free(p+1); // Freeing wrong memory block
    }

不正な Realloc アドレスパラメータ (BRP)

例:

    #include <stdlib.h>
    int main()
    {
     int *p = (int*) realloc(0,sizeof(int));
     int *q = (int*) realloc(p+20,sizeof(int[2])); // Bad address parameter for realloc 
    }

破損したガードブロック (CGB)

考えられる原因: 動的に割り当てられた配列の末尾を越えて「レッドゾーン」に書き込んでいます。

例:

    #include <stdio.h>
    #include <stdlib.h>

    int main() {
     int *p = (int *) malloc(sizeof(int)*4);
     *(p+5) = 10; //  Corrupted array guard block detected (only when the code is not annotated)
     free(p);

     return 0;
    }

メモリーの二重解放 (DFM)

考えられる原因: 同じポインタを使用して free()() を複数回呼び出しました。C++ では、同じポインタに対して delete 演算子を 2 回以上使用しています。

例:

    int *p = (int*) malloc(sizeof(int));
     free(p);
     . . .       // p was not assigned a new value between the free statements
     free(p); // Double freeing memory

解放済みメモリーの読み取り (FMR)

例:

    int *p = (int*) malloc(sizeof(int));
     free(p);
     . . .  // Nothing assigned to p in between
     printf("p = 0x%h\n",p); // Reading from freed memory

解放済みメモリーの書き込み (FMW)

例:

    int *p = (int*) malloc(sizeof(int));
     free(p);
     . . .        // Nothing assigned to p in between
     *p = 1; // Writing to freed memory 

解放済み Realloc パラメータ (FRP)

例:

   #include <stdlib.h>

   int main() {
     int *p = (int *) malloc(sizeof(int));
     free(0);
     int *q = (int*) realloc(p,sizeof(it[2])); //Freed pointer passed to realloc
   }

無効なメモリーの読み取り (IMR)

考えられる原因: ハーフワード境界、ワード境界、またはダブルワード境界に整列していないアドレスから、それぞれ 2、4、または 8 バイトを読み取っています。

例:

    #include <stdlib.h>
    int main()
    {  
      int *p = 0;
      int i = *p;   // Read from invalid memory address 
    }

無効なメモリーの書き込み (IMW)

考えられる原因: ハーフワード境界、ワード境界、またはダブルワード境界に整列していないアドレスに、それぞれ 2、4、または 8 バイトを書き込んでいます。テキストアドレスに書き込んでいるか、読み取り専用データセクション (.rodata) に書き込んでいるか、mmap によって読み取り専用にされているページに書き込んでいます。

例:

     int main()
   {
     int *p = 0;
     *p = 1;  // Write to invalid memory address 
   }

メモリーリーク

考えられる原因: メモリーが割り当てられるが、関数の終了またはエスケープの前に解放されていません。

例:

       int foo()
   {
     int *p = (int*) malloc(sizeof(int));
     if (x) {
      p = (int *) malloc(5*sizeof(int));  // will cause a leak of the 1st malloc
     }  
   }                                      // The 2nd malloc leaked here

送り側と受け側の重複 (OLP)

考えられる原因: 正しくないソース、宛先、または長さが指定されました。ソースと宛先が重複している場合、プログラムの動作は不定になります。

例:

    #include <stlib.h>
    #include <string.h>
    int main() {
      char *s=(char *) malloc(15);
      memset(s, 'x', 15);
      memcpy(s, s+5, 10);
      return 0;
    }

部分的に初期化された読み取り (PIR)

例:

    #include <stdio.h>
   #include <stdlib.h>
    int main()
   { 
     int *p = (int*) malloc(sizeof(int));
     *((char*)p) = 'c';
     printf("*(p = %d\n",*(p+1)); // Accessing partially initialized data 
   }

スタック境界を越える読み取り (SBR)

考えられる原因: ローカル配列の末尾よりあと、または先頭より前を読み取っています。

例:

    #include <stdio.h>

   int main() {
     int a[2] = {0, 1};
     printf("a[-10]=%d\n",a[-10]); //  Read is beyond stack frame bounds

     return 0;
   }

スタック境界を越える書き込み (SBW)

考えられる原因: ローカル配列の末尾よりあと、または先頭より前に書き込んでいます。

例:

    #include <stdio.h>

    int main() {
     int a[2] = {0, 1};
     a[-10] = 2; //  Write is beyond stack frame bounds

     return 0;
   }

割り当てられていないメモリーの読み取り (UAR)

考えられる原因: ストレイポインタ (不正な値を持つポインタ)、ヒープブロック境界のオーバーフロー、すでに解放されたヒープブロックへのアクセス。

例:

    #include <stdio.h>
    #include <stdlib>
   int main()
   {
     int *p = (int*) malloc(sizeof(int));
     printf("*(p+1) = %d\n",*(p+1)); // Reading from unallocated memory
   }

割り当てられていないメモリーの書き込み (UAW)

考えられる原因: ストレイポインタ (不正な値を持つポインタ)、ヒープブロック境界のオーバーフロー、すでに解放されたヒープブロックへのアクセス。

例:

    #include <stdio.h>
    #include <stdlib>
   int main()
   {
    int *p = (int*) malloc(sizeof(int)); 
     *(p+1) = 1; // Writing to unallocated memory 
  }

初期化されていないメモリーの読み取り (UMR)

考えられる原因: 初期化されていないローカルデータまたはヒープデータの読み取り。

例:

    #include <stdio.h>
    #include <stdlib>
   int main()
   {
    int *p = (int*) malloc(sizeof(int));
    printf("*p = %d\n",*p); // Accessing uninitialized data
  }