Skip to main content
本文に 371 文字追加
ソース リンク
user39889
user39889

参考情報として、使用している C コンパイラが gcc の場合、配列の範囲外アクセス(out of bounds access)に対して警告(warning)を表示するオプションが用意されています。

gcc(1)

-Warray-bounds
-Warray-bounds=n
 This option is only active when -ftree-vrp is active (default for -O2 and above). It warns about subscripts to arrays that are always out of bounds. This warning is enabled by -Wall.

-Warray-bounds=1
  This is the warning level of -Warray-bounds and is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Warray-bounds=2
  This warning level also warns about out of bounds access for arrays at the end of a struct and for arrays accessed through pointers. This warning level may give a larger number of false positives and is deactivated by default.

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ gcc -O2 -Warray-bounds -o histogram histogram.c
histogram.c: In function ‘main’:
histogram.c:27:21: warning: iteration 50 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 |     histogram[ score[ j ] ]++;
      |                ~~~~~^~~~~
histogram.c:25:3: note: within this loop
   25 |   for( j = 0; j <= NUM_SCORE; j++ )
      |   ^~~

その他に cppcheck - A tool for static C/C++ code analysis を利用しても同様の結果が得られます。

$ cppcheck --enable=all histogram.c 
Checking histogram.c ...
histogram.c:27:21: error: Array 'score[50]' accessed at index 50, which is out of bounds. [arrayIndexOutOfBounds]
    histogram[ score[ j ] ]++;
                    ^

参考情報として、使用している C コンパイラが gcc の場合、配列の範囲外アクセス(out of bounds access)に対して警告(warning)を表示するオプションが用意されています。

gcc(1)

-Warray-bounds
-Warray-bounds=n
 This option is only active when -ftree-vrp is active (default for -O2 and above). It warns about subscripts to arrays that are always out of bounds. This warning is enabled by -Wall.

-Warray-bounds=1
  This is the warning level of -Warray-bounds and is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Warray-bounds=2
  This warning level also warns about out of bounds access for arrays at the end of a struct and for arrays accessed through pointers. This warning level may give a larger number of false positives and is deactivated by default.

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ gcc -O2 -Warray-bounds -o histogram histogram.c
histogram.c: In function ‘main’:
histogram.c:27:21: warning: iteration 50 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 |     histogram[ score[ j ] ]++;
      |                ~~~~~^~~~~
histogram.c:25:3: note: within this loop
   25 |   for( j = 0; j <= NUM_SCORE; j++ )
      |   ^~~

参考情報として、使用している C コンパイラが gcc の場合、配列の範囲外アクセス(out of bounds access)に対して警告(warning)を表示するオプションが用意されています。

gcc(1)

-Warray-bounds
-Warray-bounds=n
 This option is only active when -ftree-vrp is active (default for -O2 and above). It warns about subscripts to arrays that are always out of bounds. This warning is enabled by -Wall.

-Warray-bounds=1
  This is the warning level of -Warray-bounds and is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Warray-bounds=2
  This warning level also warns about out of bounds access for arrays at the end of a struct and for arrays accessed through pointers. This warning level may give a larger number of false positives and is deactivated by default.

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ gcc -O2 -Warray-bounds -o histogram histogram.c
histogram.c: In function ‘main’:
histogram.c:27:21: warning: iteration 50 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 |     histogram[ score[ j ] ]++;
      |                ~~~~~^~~~~
histogram.c:25:3: note: within this loop
   25 |   for( j = 0; j <= NUM_SCORE; j++ )
      |   ^~~

その他に cppcheck - A tool for static C/C++ code analysis を利用しても同様の結果が得られます。

$ cppcheck --enable=all histogram.c 
Checking histogram.c ...
histogram.c:27:21: error: Array 'score[50]' accessed at index 50, which is out of bounds. [arrayIndexOutOfBounds]
    histogram[ score[ j ] ]++;
                    ^
ソース リンク
user39889
user39889

参考情報として、使用している C コンパイラが gcc の場合、配列の範囲外アクセス(out of bounds access)に対して警告(warning)を表示するオプションが用意されています。

gcc(1)

-Warray-bounds
-Warray-bounds=n
 This option is only active when -ftree-vrp is active (default for -O2 and above). It warns about subscripts to arrays that are always out of bounds. This warning is enabled by -Wall.

-Warray-bounds=1
  This is the warning level of -Warray-bounds and is enabled by -Wall; higher levels are not, and must be explicitly requested.

-Warray-bounds=2
  This warning level also warns about out of bounds access for arrays at the end of a struct and for arrays accessed through pointers. This warning level may give a larger number of false positives and is deactivated by default.

$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ gcc -O2 -Warray-bounds -o histogram histogram.c
histogram.c: In function ‘main’:
histogram.c:27:21: warning: iteration 50 invokes undefined behavior [-Waggressive-loop-optimizations]
   27 |     histogram[ score[ j ] ]++;
      |                ~~~~~^~~~~
histogram.c:25:3: note: within this loop
   25 |   for( j = 0; j <= NUM_SCORE; j++ )
      |   ^~~