Patchwork [libgcc] : Avoid warning: array subscript is above array bounds when compiling crtstuff.c

login
register
mail settings
Submitter Uros Bizjak
Date March 9, 2014, 1:57 p.m.
Message ID <CAFULd4Y6p0kCyyE3nEnOFTmWhv785++E4dOh=HWZfX+sjFKkCQ@mail.gmail.com>
Download mbox | patch
Permalink /patch/328327/
State New
Headers show

Comments

Uros Bizjak - March 9, 2014, 1:57 p.m.
On Fri, Mar 7, 2014 at 8:03 PM, Ian Lance Taylor <iant@google.com> 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 don't understand why this works.  You can't index element 0 of a 0
> element array.
>
> This code is relying on linker magic and it's not surprising that it
> confuses the compiler.  Can you use a type cast or variable assignment
> in frame_dummy instead?

I was not able to cast variable with any sensible type cast (maybe I
didn't understand your suggestion correctly), the only other variant
that avoided warning was

--cut here--
--cut here--

but I don't know if this is correct approach, concerning mentioned linker magic.

I have opened PR 60472 [1] with a reduced testcase, if somebody wants
to deal with this problem...

[1] http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60472

Uros.
Ian Taylor - March 9, 2014, 4:41 p.m.
On Sun, Mar 9, 2014 at 6:57 AM, Uros Bizjak <ubizjak@gmail.com> wrote:
> On Fri, Mar 7, 2014 at 8:03 PM, Ian Lance Taylor <iant@google.com> 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 don't understand why this works.  You can't index element 0 of a 0
>> element array.
>>
>> This code is relying on linker magic and it's not surprising that it
>> confuses the compiler.  Can you use a type cast or variable assignment
>> in frame_dummy instead?
>
> I was not able to cast variable with any sensible type cast (maybe I
> didn't understand your suggestion correctly), the only other variant
> that avoided warning was
>
> --cut here--
> Index: crtstuff.c
> ===================================================================
> --- crtstuff.c  (revision 208403)
> +++ crtstuff.c  (working copy)
> @@ -259,7 +259,7 @@
>     so we can register them properly.  */
>  STATIC void *__JCR_LIST__[]
>    __attribute__ ((used, section(JCR_SECTION_NAME), aligned(sizeof(void*))))
> -  = { };
> +  = { 0 };
>  #endif /* JCR_SECTION_NAME */
>
>  #if USE_TM_CLONE_REGISTRY
> --cut here--
>
> but I don't know if this is correct approach, concerning mentioned linker magic.

As you guess, that likely won't work, because it will introduce a NULL
pointer into the array.

Ian
Jakub Jelinek - March 9, 2014, 5:31 p.m.
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__.

	Jakub

Patch

Index: crtstuff.c
===================================================================
--- crtstuff.c  (revision 208403)
+++ crtstuff.c  (working copy)
@@ -259,7 +259,7 @@ 
    so we can register them properly.  */
 STATIC void *__JCR_LIST__[]
   __attribute__ ((used, section(JCR_SECTION_NAME), aligned(sizeof(void*))))
-  = { };
+  = { 0 };
 #endif /* JCR_SECTION_NAME */

 #if USE_TM_CLONE_REGISTRY