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

--- Comment #3 from Alejandro Colomar <alx at kernel dot org> ---
(In reply to Jakub Jelinek from comment #1)
> How is compound literal different from any other automatic variable?
> And of course taking the address of compound literal when used in scope is
> just fine and heavily used in real-world.

Automatic variables are declared in a block that fully contains the call in
which it is used.

Compound literals are not declared, and thus using them inside a macro that
contains ({}) will result in a surprisingly shorter lifetime.


alx@devuan:~/tmp$ cat cl.c | grep -Tn ^
                  1:    void *f(void *p) {return p;}
                  2:
                  3:    #define g(x) ({ f(x); })
                  4:
                  5:    int
                  6:    main(void)
                  7:    {
                  8:            int  i = 7;
                  9:            int  *p;
                 10:
                 11:            // Okay
                 12:            p = g(&i);
                 13:
                 14:            // Not okay
                 15:            p = g(&(int){42});
                 16:
                 17:            return *p;
                 18:    }
alx@devuan:~/tmp$ gcc -Wall -Wextra cl.c 
alx@devuan:~/tmp$ ./a.out; echo $?
42

AFAIK, this program has UB, and that 42 is unreliable.

Reply via email to