This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: odd array subscript is above array bounds error
- From: Dave Korn <dave dot korn dot cygwin at googlemail dot com>
- To: Jack Howarth <howarth at bromo dot med dot uc dot edu>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 05 Mar 2009 03:56:18 +0000
- Subject: Re: odd array subscript is above array bounds error
- References: <20090305030005.GA23523@bromo.med.uc.edu>
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