Re: GCC labels as values extension and scope

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 11 Apr 2017, sztfg@xxxxxxxxx wrote:
> #include <stdio.h>
> 
> void *labels[3];
> 
> void test(void *ptr)
> {
>     if(ptr == NULL)
>     {
>       labels[0] = &&l1;
>       labels[1] = &&l2;
>       labels[2] = &&l3;
>       return;
>     }
>     goto *ptr;
> l1:
>     printf("test1\n");
>     return;
> l2:
>     printf("test2\n");
>     return;
> l3:
>     printf("test3\n");
>     return;
> }
> 
> int main(void)
> {
>    test(NULL);
>    test(labels[0]);
>    test(labels[1]);
>    test(labels[2]);
> }
> 
> It is possible to get label pointer without adding this if(ptr == NULL) {
> blah-blah } stuff and global array?

No. Also note that this code is _not_ guaranteed to work as expected in recent
releases of GCC, because, in short, the compiler treats labels similarly to
automatic variables, meaning that their address is considered valid only
until the function returns.  Optimizations that clone functions need to
duplicate labels, and that will break code that smuggles label addresses
to outer frames.  See PR 80053 comment 2 for a sample that gets "miscompiled":
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80053#c2

HTH
Alexander



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux