This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH, libgcc]: Avoid warning: array subscript is above array bounds when compiling crtstuff.c
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Uros Bizjak <ubizjak at gmail dot com>
- Cc: Ian Lance Taylor <iant at google dot com>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 10 Mar 2014 08:49:32 +0100
- Subject: Re: [PATCH, libgcc]: Avoid warning: array subscript is above array bounds when compiling crtstuff.c
- Authentication-results: sourceware.org; auth=none
- References: <CAFULd4awVFMCqOP48Sq_1mqWOKZky1RvfGAsAHjhQONzM=4FAg at mail dot gmail dot com> <CAKOQZ8w3K_zaKtPuPZz=Jn4TTP2BbFrroKQYkn3ZDO6Z8yoqPA at mail dot gmail dot com> <CAFULd4Y6p0kCyyE3nEnOFTmWhv785++E4dOh=HWZfX+sjFKkCQ at mail dot gmail dot com> <CAKOQZ8y9KVPSKt+MjrL-YVoq+ZJr93ag7T=E-ZJorKPJ=HYu1g at mail dot gmail dot com> <20140309173142 dot GP22862 at tucnak dot redhat dot com> <CAFULd4apzqyXy+t8uGt=rwR+rQGYxrVDY+-UVWc+wa-3iw3fcw at mail dot gmail dot com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Sun, Mar 09, 2014 at 10:38:25PM +0100, Uros Bizjak wrote:
> On Sun, Mar 9, 2014 at 6:31 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> > On Sun, Mar 09, 2014 at 09:41:59AM -0700, Ian Lance Taylor wrote:
> >> >>> Attached patch avoids a bunch of:
> >> >>>
> >> >>> ../../../gcc-svn/trunk/libgcc/crtstuff.c: In function 'frame_dummy':
> >> >>> ../../../gcc-svn/trunk/libgcc/crtstuff.c:463:19: warning: array
> >> >>> subscript is above array bounds [-Warray-bounds]
> >> >>> if (__JCR_LIST__[0])
> >> >>> ^
> >> >>>
> >> >>> when compiling libgcc.
> >> >>>
> >> >>> 2014-03-08 Uros Bizjak <ubizjak@gmail.com>
> >> >>>
> >> >>> * crtstuff.c (__JCR_LIST__): Declare as zero-length array.
> >
> > I guess the only thing to avoid the warning (and potential miscompilation)
> > is to hide the access from the optimizers through something like:
> > void *jcr_list;
> > __asm ("" : "=g" (jcr_list) : "0" (__JCR_LIST__));
> > and then use jcr_list instead of __JCR_LIST__.
>
> Attached patch builds on your idea, but jcr_list temporary has to be
> declared as void ** to allow proper dereference of pointer to void
> array.
>
> The resulting code is also a bit better, as shown by following test:
Well, better is non-obvious, while it is smaller (which is good for
initialization and thus rarely executed code), the common case is that
*jcr_list is 0 (gcj is used rarely these days) and for the common case it is
one instruction longer.
Perhaps at least use if (__builtin_expect (*jcr_list != NULL, 0))?
Otherwise looks good to me.
Jakub