why -> warning: array subscript is above array bounds

Jim Wilson jim.wilson@linaro.org
Fri May 29 22:09:00 GMT 2015


On 05/28/2015 12:41 PM, 田部 wrote:
> gcc is, in this code, or put out a warning?
> -O4 only???

-O3 and higher enables more aggressive loop unrolling.  This happens in
the pass_complete_unrolli function.  With -O3 or higher, the inner loop
is unrolled 12 times because the array size is 12.  The array bounds
checking code then notices that the last 6 copies of the unrolled loop
body access past the end of the loop and emits warnings for them.  Thus
you get 6 warnings.  Without the unrolling, we don't have enough info to
generate any warnings.  This is odd behaviour, but I'm not sure if it
should be a bug or not.  Perhaps just an enhancement request to make it
work better.

> + cat -n bug.c
>      1    #include <stdio.h>
>      2    int    ary[2][12] ;
>      3    void    func(int    x,int     y)
>      4    {
>      5        int    i,j ;
>      6        for(i = 0 ; i < x ; i++) {
>      7            for(j = 0 ; j < y ; j++) {
>      8                ary[i][j + y]  = 0 ;
>      9            }
>     10        }
>     11    }

The code is potentially unsafe, because there are no array bounds checks
in here.  If I add a line
    assert (x <= 2 && y <= 6);
and include assert.h then the warnings go away at -O3.

FYI This mailing list is primarily for email from our bugzilla bug
database.  It is better to file a bug into bugzilla than to send email
here, as email here isn't tracked.

Jim



More information about the Gcc-bugs mailing list