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

            Bug ID: 96369
           Summary: Wrong evaluation order of || operator
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frank_yzg at outlook dot com
  Target Milestone: ---

The following code snippet:

#include <stdio.h> 
int main(void)
{
    const long ONE = 1L;
    long y = 0L;
    long x = ((long) (ONE || (y = 1L)) % 8L);
    printf("x = %ld, y = %ld\n", x, y);    // with -O1/-O2/-O3 flag, gcc prints
x = 1, y = 1 where clang prints x = 1, y = 0
}

> $ /usr/gcc-trunk/bin/gcc -O2 -Wall -Wextra bug.c -o a.out
> $ ./a.out
> x = 1, y = 1

If the first operand of a logical-OR operation has a nonzero value, the second
operand is not evaluated. Therefore (y = 1L) should not be evaluated. It should
print y = 0 instead of y = 1.

This bug appears in GCC-4.8, GCC-6.4.0, GCC-7.5.0, GCC-8.4.0, GCC-9.3.0,
GCC-10.2.0 and GCC-11.0.0 20200501.

Reply via email to