Is it possible to enable array bounds checking in

2019-01-24 02:11发布

Is it possible to have g++ show an error when compiling the following file with some flag?

#include <iostream>
using namespace std;

int main()
{
   int arr[ 2 ];

   cout << arr[ 4 ] << endl;

   return 0;
}

I saw some things like gcc -Wall -O2 main.c which only works with C, not C++.

标签: g++ c++
5条回答
祖国的老花朵
2楼-- · 2019-01-24 02:54

For raw arrays, I don't think so, because -fbounds-check didn't work with your example and MingW g++ 4.4.1, and because the old 3.x docs I have say

-fbounds-check

For front-ends that support it, generate additional code to check that indices used to access arrays are within the declared range. This is currently only supported by the Java and Fortran 77 front-ends, where this option defaults to true and false respectively.

However, with std::vector you can use at to have a slightly impractical run-time bounds-checking (generates exception). And you can use a special debug version of the standard library, that provides practical run-time bounds-checking for []. For example, when compiling…

#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<int>  arr( 2 );

   cout << arr[ 4 ] << endl;
}

… you get different respectively non-checking and checking behavior for the release and debug versions of the g++ standard library implementation:

C:\test> g++ x.cpp & a
4083049

C:\test> g++ x.cpp -D _GLIBCXX_DEBUG -D _GLIBCXX_DEBUG_PEDANTIC & a
c:\program files\mingw\bin\../lib/gcc/mingw32/4.4.1/include/c++/debug/vector:265:
    error: attempt to subscript container with out-of-bounds index 4, but
    container only holds 2 elements.

Objects involved in the operation:
sequence "this" @ 0x0x22ff1c {
  type = NSt7__debug6vectorIiSaIiEEE;
}

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

C:\test> _

Reportedly for newer g++ versions (after 4.0) you don't need the _GLIBCXX_DEBUG_PEDANTIC symbol. For details, see the GNU documentation.

Cheers & hth.,

查看更多
看我几分像从前
3楼-- · 2019-01-24 03:00

I recall seeing a gcc or g++ warning message from ffmpeg or x264 along the lines of

"warning index of array may be out of bounds"

http://gcc.gnu.org/ml/gcc/2000-07/msg01000.html

seems like it probably made it in.

The constraint is that you have an example like yours above. as soon as you have have variables instead of literals it is not possible. except perhaps in a simple loop.

查看更多
Rolldiameter
4楼-- · 2019-01-24 03:01

Not at compile time. You might be able to check that at runtime though.

For that take a look at: Runtime array bounds checking with g++

查看更多
别忘想泡老子
5楼-- · 2019-01-24 03:06

You can replace arrays with std::vector. Vector has accessor member function (std::vector::at) that does bounds checking at runtime.

Compile time check for buffer overflow is a very hard undecidable problem, unfortunately. It's usually handled by a full blown static analysis tool.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-24 03:09

You can use a static analyser such as Cppcheck. When run on your above code:

$ cppcheck --enable=all test.cpp
Checking test.cpp...
[test.cpp:6]: (style) Variable 'arr' is not assigned a value
[test.cpp:8]: (error) Array 'arr[2]' index 4 out of bounds

You can integrate Cppcheck into your build procedure and consider your code built successfully only if Cppcheck passes.

查看更多
登录 后发表回答