[Bug c/35587] New: -Warray-bounds does not work at all or does not find all trivial cases, and works only with -O2 or -O3

karsten_burger at gmx dot de gcc-bugzilla@gcc.gnu.org
Fri Mar 14 17:05:00 GMT 2008


-Warray-bound is really a very interesting new feature, since me and some
colleagues recently spent many hours debugging a large legacy software, and
finally found C-char-array overrun errors as causes in most cases.

I used the following example:

#include <stdio.h>
int main()
{
  int a[5];
  a[-1] = 0;                  // line 5: error
  a[+1] = 0;  // ok
  a[+5] = 0;                  // line 7: error
  a[15] = 0;                  // line 8: error
  printf("%d \n", a[15]);     // line 9: error
  int i=15;
  printf("%d \n", a[i]);      // line 11: error
  return 0;
}

I used the following command line:
    /usr/local/gcc-4.3.0/bin/gcc-4.3.0  bounds.c -O2 -Warray-bounds

1. Test: 
  I get no warning at compile time. I retried it using -O0, -O1, -O3, and there
 is also no warning.

2. Test:
  I changed integer i in line 10 to a value of 16.
  Now I correctly get warnings in lines 5, 7, 8, 11, but not in line 9, stating
that the index is below/larger than the array permits.
  Interestingly, the warnings are only issued if I am using -O2 or -O3, but not
with -O0 and not with -O1.


Conclusion:
* -Warray-bound is not reliable at the moment, it may or may not issue warnings
or no warnings at all.
* When it issues warnings, at line 8 of the example it finds the incorrect
assignment to a[15], but at line 9 not the incorrect use of a[15]. 
* When it issues warnings and I change line 9 to a[16], it reports lines 5, 7,
8, 9 but not line 11.
* If this option works only with -O2 and -O3 due to technical reasons, it
should be documented clearly to avoid frustration of the user.


-- 
           Summary: -Warray-bounds does not work at all or does not find all
                    trivial cases, and works only with -O2 or -O3
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: karsten_burger at gmx dot de
 GCC build triplet: i686-linux-gnu
  GCC host triplet: i686-linux-gnu
GCC target triplet: i686-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35587



More information about the Gcc-bugs mailing list