ABR: 配列境界を越える読み取り (beyond array bounds read)
ABW: 配列境界を越える書き込み (beyond array bounds write)
DFM: メモリーの二重解放 (double freeing memory)
ECV: 明示的型キャスト違反 (explicit type cast violation)
FMR: 解放済みメモリーの読み取り (freed memory read)
FMW: 解放済みメモリーの書き込み (freed memory write)
INF: 空の無限ループ (infinite empty loop)
MLK: メモリーリーク (memory leak)
MFR: 関数の復帰なし (missing function return)
MRC: malloc 戻り値の検査なし (missing malloc return value check)
NFR: 初期化されていない関数の復帰 (uninitialized function return)
NUL: NULL ポインタ間接参照、リークの可能性があるポインタの検査
RFM: 解放済みメモリーを返す (return freed memory)
UMR: 初期化されていないメモリーの読み取り、初期化されていないメモリーの読み取りビット操作 (uninitialized memory read, uninitialized memory read bit operation)
URV: 使用されていない戻り値 (unused return value)
VES: スコープ外での局所変数の使用 (out-of-scope local variable usage)
このセクションでは、エラーの考えられる原因と、エラーが発生する可能性があるコードの例を説明します。
考えられる原因: 配列境界を越えてメモリーを読み取ろうとしました。
例:
int a[5];
. . .
printf("a[5] = %d\n",a[5]); // Reading memory beyond array bounds
考えられる原因: 配列境界を越えてメモリーに書き込もうとしました。
例:
int a [5];
. . .
a[5] = 5; // Writing to memory beyond array bounds
考えられる原因: 同じポインタを使用して free()() を複数回呼び出しました。C++ では、同じポインタに対して delete 演算子を 2 回以上使用しています。
例:
int *p = (int*) malloc(sizeof(int));
free(p);
. . . // p was not signed a new value between the free statements
free(p); // Double freeing memory
例:
int *p = (int*) malloc(sizeof(int));
free(p);
. . . // Nothing assigned to p in between
int j = *p; // Reading from freed memory
例:
int *p = (int*) malloc(sizeof(int));
free(p);
. . . // Nothing assigned to p in between
*p = 1; // Writing to freed memory
例:
int x=0;
int i=0;
while (i200) {
x++; } // Infinite loop
考えられる原因: メモリーが割り当てられるが、関数の終了またはエスケープの前に解放されていません。
例:
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
考えられる原因: 終了するパスの一部に戻り値がありません。
例:
#include <stdio.h>
int foo (int a, int b)
{
if (a)
{
return b;
}
} // If foo returns here, the return is uninitialized
int main ( )
{
printf("%d\n", foo(0,30));
}
考えられる原因: Cの malloc または C++ の new 演算子からの戻り値に null 検査なしでアクセスします。
例:
#include <stdlib.h>
int main()
{
int *p3 = (int*) malloc(sizeof(int)); // Missing null-pointer check after malloc.
*p3 = 0;
}
考えられる原因: null に等しくなる可能性のあるポインタにアクセスしているか、null になることのないポインタに冗長な null 検査を行なっています。
例:
#include <stdio.h>
#include <stdlib.h>
int gp, ctl;
int main()
{
int *p = gp;
if (ctl)
p = 0;
printf ("%c\n", *p); // May be null pointer dereference
if (!p)
*p = 0; // Surely null pointer dereference
int *p2 = gp;
*p2 = 0; // Access before checking against NULL.
assert (p2!=0);
int *p3 = gp;
if (p3) {
printf ("p3 is not zero.\n");
}
*p3 = 0; // Access is not protected by previous check against NULL.
}
例:
#include <stdlib.h>
int *foo ()
{
int *p = (int*) malloc(sizeof(int));
free(p);
return p; // Return freed memory is dangerous
}
int main()
{
int *p = foo();
*p = 0;
}
考えられる原因: 初期化されていないローカルデータまたはヒープデータの読み取り。
例:
#include <stdio.h>
#include <stdlib.h>
struct ttt {
int a: 1;
int b: 1;
};
int main()
{
int *p = (int*) malloc(sizeof(int));
printf("*p = %d\n",*p); // Accessing uninitialized data
struct ttt t;
extern void foo (struct ttt *);
t.a = 1;
foo (&t); // Access uninitialized bitfield data "t.b"
}
考えられる原因: 初期化されていないローカルデータまたはヒープデータの読み取り。
例:
int foo();
int main()
{
foo(); // Return value is not used.
}
考えられる原因: 初期化されていないローカルデータまたはヒープデータの読み取り。
例:
int main()
{
int *p = (int *)0;
void bar (int *);
{
int a[10];
p = a;
} // local variable 'a' leaked out
bar(p);
}