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

            Bug ID: 116355
           Summary: switchconv introduces new signed overflow UB
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
  Target Milestone: ---

Consider the function below (taken from gcc.dg/tree-ssa/switch-3.c):

int cipher_to_alg(int cipher)        
{                                    
  switch (cipher)              
    {                            
      case 8:   return 2;  
      case 16:  return 3;  
      case 32:  return 4;  
      case 64:  return 6;  
      case 256: return 9;  
      case 512: return 10; 
      case 2048: return 11;
      case 4096: return 12;
      case 8192: return 13;
    }                            
  return 0;                    
}     

Compiling this with -O2 on X86_64 introduces new UB when the switchconv pass
changes the code to:

  _12 = -cipher_2(D);
  _11 = cipher_2(D) & _12;
  _10 = _11 == cipher_2(D);
  if (_10 != 0)
    goto <bb 3>; [50.00%]
  else
    goto <bb 6>; [50.00%]

Here, _12 is a signed value that overflows when cipher is INT_MIN.

Reply via email to