How to fix 'array subscript is above array bounds' ?

Ian Lance Taylor iant@google.com
Tue Apr 3 18:05:00 GMT 2012


David Aldrich <David.Aldrich@EMEA.NEC.COM> writes:

> Actually I have a harder example of this warning to fix.  What should one do in this situation please?
>
> unsigned rsCodeTable( unsigned a_rsGroup, unsigned a_sc )
> {
>         static const unsigned PUCCHIP_RSCodeTable[30][12] = {
>         {2, 1, 0, 3, 0, 0, 1, 1, 0, 1, 3, 0},
>         {1, 1, 0, 0, 0, 2, 1, 3, 3, 1, 3, 0},
>         {1, 1, 3, 3, 3, 2, 3, 3, 1, 3, 1, 2},
>         {2, 1, 1, 1, 1, 2, 3, 3, 1, 3, 0, 2},
>         {2, 0, 1, 2, 1, 2, 3, 2, 1, 2, 1, 0},
>         {1, 3, 0, 2, 2, 1, 1, 2, 2, 0, 3, 1},
>         {2, 0, 3, 3, 3, 0, 1, 2, 0, 0, 3, 1},
>         {3, 2, 2, 2, 1, 3, 0, 2, 1, 3, 0, 1},
>         {1, 3, 0, 1, 2, 2, 2, 1, 1, 0, 2, 1},
>         {1, 3, 2, 0, 0, 2, 3, 1, 1, 1, 1, 1},
>         {2, 0, 2, 1, 1, 3, 3, 2, 3, 3, 0, 2},
>         {0, 1, 2, 2, 0, 0, 3, 1, 0, 1, 0, 0},
>         {1, 3, 1, 1, 3, 1, 1, 1, 3, 3, 3, 1},
>         {0, 0, 3, 0, 3, 1, 1, 0, 2, 3, 0, 0},
>         {3, 1, 2, 3, 2, 0, 1, 0, 0, 0, 2, 1},
>         {0, 2, 1, 3, 2, 2, 1, 1, 0, 1, 2, 3},
>         {1, 0, 1, 2, 1, 0, 0, 0, 2, 2, 0, 2},
>         {3, 1, 1, 0, 3, 0, 3, 3, 0, 1, 0, 2},
>         {3, 0, 1, 1, 3, 1, 3, 3, 2, 2, 1, 3},
>         {2, 0, 1, 0, 1, 2, 2, 0, 3, 2, 3, 2},
>         {2, 3, 1, 1, 1, 1, 0, 1, 2, 1, 3, 2},
>         {2, 0, 2, 1, 3, 3, 3, 3, 3, 1, 2, 3},
>         {1, 1, 3, 3, 3, 3, 2, 0, 3, 1, 3, 0},
>         {1, 1, 2, 3, 2, 3, 1, 2, 1, 0, 2, 1},
>         {1, 1, 0, 1, 0, 0, 2, 1, 2, 3, 3, 1},
>         {1, 3, 0, 0, 1, 0, 0, 1, 3, 2, 2, 0},
>         {1, 0, 3, 3, 0, 3, 1, 2, 2, 0, 2, 3},
>         {3, 2, 3, 2, 3, 0, 1, 2, 1, 0, 3, 3},
>         {2, 0, 3, 0, 2, 0, 0, 3, 0, 0, 2, 2},
>         {0, 3, 3, 2, 2, 3, 2, 0, 3, 0, 1, 2} };
>     if ( a_rsGroup >= 30 ) fatal("PUCCHIP::rsCodeTable() : Illegal RS group number");
>     if ( a_sc      >= 12 ) fatal("PUCCHIP::rsCodeTable() : Illegal subcarrier number");
>
>     return PUCCHIP_RSCodeTable[a_rsGroup][a_sc];    <<== WARNING  'array subscript is above array bounds'
> }

Mark the function fatal with __attribute__ ((noreturn)).  Or change this
code to explicitly return.

Either way, GCC appears to have found some code which is invoking this
function without an out of range subscript.

Ian



More information about the Gcc-help mailing list