はじめに
C++の静的解析ツール「cppcheck」でソースコードを静的解析した場合に、以下の警告がでることがあります。
error: Array 'xxx[n]' accessed at index n, which is out of bounds.
cppcheckのバージョン
- v1.65
サンプルプログラム
以下のソースプログラムを解析にかけると表示されます。
int _tmain(int argc, _TCHAR* argv[]) { char tmp[5]; memcpy(tmp, "hoge", 4); tmp[5] = 0x00; // NG tmp[4] = 0x00; // OK return 0; }
上記の感じで、配列の末尾をNULL止めしようとして、誤って要素数を超えた指定をした場合に警告が表示されます。
関連記事
- C6305の解説 sizeof 数と countof 数の間で不一致が発生した可能性があります
- 【cppcheck】Variable 'xxx' is reassigned a value before the old one has been used.
- Visual C++ 2013でDLLを作成して動的読み込みしてみる。
- 【cppcheck】The code contains characters that are unhandled. Neither unicode nor extended ASCII are supported.
- 【cppcheck】Size of pointer 'xxx' used instead of size of its data.