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

            Bug ID: 93301
           Summary: Wrong optimization: instability of uninitialized
                    variables leads to nonsense
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ch3root at openwall dot com
  Target Milestone: ---

Instability is inconsistency, which leads to logical contradictions, which
leads to total chaos. Similar to bug 61502, comment 42, but with uninitialized
variables:

----------------------------------------------------------------------
#include <stdio.h>

__attribute__((noipa)) // imagine it in a separate TU
static void *opaque(void *p) { return p; }

int main()
{
    int c = 1;
    opaque(&c);

    unsigned char x = 0;
    opaque(&x);
    unsigned char y; // no trap representation possible
    (void)&y; // disarm C11, 6.3.2.1p2

    unsigned char z;
    int b;
    if (x == y) {
        b = 1;
        z = x;
    } else {
        b = 0;
        z = y;
    }

    opaque(&b);
    if (b)
        printf("b = %d  c = %d  x = %d  e = %d\n", b, c, x, c ? z : 5);
}
----------------------------------------------------------------------
$ gcc -std=c11 -O3 test.c && ./a.out
b = 1  c = 1  x = 0  e = 5
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.1 20200117 (experimental)
----------------------------------------------------------------------

Given that the printf has fired, `b` is `1`, hence `z` is the same as `x` and
`e = 0` should be printed.

According to my reading of C11 this program doesn't invoke UB. (And I thought
that most proposals about "wobbly" values wouldn't change this but I'm not sure
anymore:-)

Even if this particular example is deemed undefined by gcc, I guess
inconsistencies could blow everything up even without any help from a
programmer.

clang bug -- https://bugs.llvm.org/show_bug.cgi?id=44512.

Reply via email to