https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106842

            Bug ID: 106842
           Summary: misleading warning : iteration X invokes undefined
                    behavior
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: franckbehaghel_gcc at protonmail dot com
  Target Milestone: ---

Hello,

I get the following misleading warning with gcc 12.2 with -O2/-O3.
11.3 seems fine.

gcc -O2 main.c 
main.c: In function ‘main’:
main.c:16:38: warning: iteration 9 invokes undefined behavior
[-Waggressive-loop-optimizations]
   16 |            for(int64_t k =0; k<i1 ; k++)
      |                                     ~^~
main.c:16:31: note: within this loop
   16 |            for(int64_t k =0; k<i1 ; k++)
      |                              ~^~~

cat main.c
#include "stdio.h"
#include "stdint.h"

int main(int argc, char** argv)
{
   int64_t i1=0;
   int64_t i3=0,i2=0; // warning with this declaration order 
   //int64_t i2=0,i3=0; // but fine (no warning) with this order

   for ( i1 = 0; i1<10 ; i1++)
   {
       for ( ; i2<10 ;i2++ )         
           printf("L2\n");

       for ( ; i3<10 ; i3++ )
           for(int64_t k =0; k<i1 ; k++)
               printf("L3\n");
   }

   printf("i1 %lu i2 %lu i3 %lu\n",i1,i2,i3);  
   return 0;
}


Could someone reproduce ?
The weirdest part of this warning : it depends on the index declaration order.
Even if gcc has a bad(?) way of computing internal range/validity to deduce
such issues, I would expect it in both order.

Regards,

Reply via email to