[gnu.gcc.bug] Bugreport for gcc v1.35

anders@idt.unit.no (Anders Christensen) (06/19/89)

I think I have come over a bug in gcc. The following program seems to
produce code which executes incorrectly.

#include <stdio.h>
main()
{
  int i=0, j=10, k=100, l ;
  k += ((i=0) ? (j--) : 0) ;        /* incorrectly executed statement */
  printf("j should be 10, but seems to be %d\n", j ) ;  /* writes '9' */
  exit(0) ;
}

What happens is that the expression ((i=0) ? (j--) : 0) decrements
the variable 'j'. I believe that the (j--) part of the statement never
should be evaluated. The program will execute correctly (i.e. the 
variable 'j' will continue to have the value 10) if I substitute the
offending line with any of these:

  k  = ((i=0) ? (j--) : 0)     ;
  k += (  0   ? (j--) : 0)     ;
  k += ((i=0) ? (j--) : (l=0)) ;
  
I have tested it on the following machines, and observed the bug. I have
used no special options on the command line ( gcc -o test test.c )

  - gcc v1.34 on a Sun 3/60 running SunOS v4.0.1
  - gcc v1.35 on a Sun 3/280 running SunOS v3.5
  - gcc v1.35 on a VAX 11/780 running Ultrix v2.2

I hope this will help you to find and remove this bug. I will also like
to thank you for a *great* compiler, which is far better than most 
commercially available software.

Anders Christensen  ( achriste@norunit.bitnet,  anders@idt.unit.no )
Norwegian Institute of Technology, Trondheim