This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: odd array subscript is above array bounds error


Jack Howarth wrote:
> Dave,
>     My original guess was that as well, however when I tried a simplier test
> case of...
> 
> #define CMD_QUEUE_MASK 0x3
> void test()
> {
>     int cmdQueue[CMD_QUEUE_MASK+1];
> 
>     int a;
> 
>     for(a=0;a<=CMD_QUEUE_MASK;a++)
>       ;
>     cmdQueue[a] = 0;
> }
> 
> compiled with -O3 -Wall, the "array subscript is above array bounds"
> warning wasn't triggered for that case. Odd.

  C'mon, Jack, you /know/ how arrays work in C.  You *know* it's an out of
bounds access.  cmdQueue[4] has 4 entries numbered 0 to 3.  The loop continues
until a is no longer <= 3, i.e. a is 4 when it exits.  Accessing subscript [4]
of an array that only has size[4] is going one past the end.  So the bug is
the missing warning for the simplified testcase, not that the warning is
somehow incorrect in the more complex one.

  I would hope that in the simpler case the entire unused local array gets
optimised away, and the loop and 'a' go with it.  That might explain why
there's no sign of an error.  But your testcase invokes invalid behaviour, so
there's no reason why the compiler shouldn't handle it in differing and
inconsistent ways at different optimisation levels.

  Either that or I'm *really* half-asleep now.  I always mistrust something
that seems obvious to me but not to someone else when I'm tired.  Am I missing
something here?

    cheers,
      DaveK


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]