5

Having tried this

int main(void) {

int a[10]; a[20]=5;

}

gcc -Wall -O2 main.c

It gives me no warning...

It's gcc within windows (mingw) and I am not able to detect this kind of boundary limit bug

how to tell compiler to check it? can mingw do it?

thanks

Hubert Kario
  • 21,314
  • 3
  • 24
  • 44
Hernán Eche
  • 6,529
  • 12
  • 51
  • 76

3 Answers3

2

There are attempts to deal with array bounds checking. By default, the santdard C99 says nothing about enforcing array bounds, I believe largely because it has more overhead.

That being said you can look at sites like this where people have tried to deal with it:

http://williambader.com/bounds/example.html

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • Good this project http://sourceforge.net/projects/boundschecking/ have "patches" for gcc, to add -fbounds-checking flag, I think they are only for linux, but it's a start.. – Hernán Eche Jun 10 '10 at 13:53
1

The question is quite old but I think another answer I got here. Even though if you plan to do a full build on gcc, you can give a try to clang compiler (e.g. for nightly builds in large projects). It worked quite well.

An example is the output when I tried accessing index 4 where the array size is declared as 4:

    ex9.c:17:2: warning: array index 4 is past the end of the array (which contains
          4 elements) [-Warray-bounds]
            numbers[4] = 4;
            ^       ~
    ex9.c:4:2: note: array 'numbers' declared here
            int numbers[4] = {0};
            ^

Thanks Kajal

Kajal Sinha
  • 1,565
  • 11
  • 20
1

There are other non-compiler tools that can use static analysis to find errors like array boundary violations. A previous SO question discusses some of them. Mind you, if you're needing to run in a mingw environment that may limit your choices.

Community
  • 1
  • 1
torak
  • 5,684
  • 21
  • 25