On Thu, Mar 05, 2009 at 03:56:18AM +0000, Dave Korn wrote:
> Jack Howarth wrote:
>
> > Ortho.c: In function 'OrthoFree':
> > Ortho.c:1973: warning: array subscript is above array bounds
>
> > #define CMD_QUEUE_MASK 0x3
>
> > CQueue *cmdQueue[CMD_QUEUE_MASK+1];
>
> > int a;
> >
> > for(a=0;a<=CMD_QUEUE_MASK;a++)
> > ;
> > I->cmdQueue[a] = ((void *)0);
> > }
> >
> > which produces the warning...
> >
> > test.c: In function ‘OrthoFree’:
> > test.c:24: warning: array subscript is above array bounds
> >
> > ...for the last assignment of I->cmdQueue[a] = ((void *)0).
> > It would seem to me that 'a' should be CMD_QUEUE_MASK+1
> > on exiting the for statement so that the assignment should
> > be valid.
>
> Am I tired or are you? This looks like a typical off-by-one in the code to
> me. CMD_QUEUE_MASK is 0x3, meaning that the array is cmdQueue[4], consisting
> of entries numbered 0 ... 3. The loop exits when is no longer <= 3, i.e. a ==
> 4. That's not a valid index between 0 and 3.
>
> OTOH it is 4am over here and I could be blind. I'll just go over here now
> and zzzzzzzzzzzzzzzzzzzzzzzZZZZZZSNRK
>
> cheers,
> DaveK
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.
Jack