Re: GCC optimization re-order statements

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

 



On Fri, 2021-06-11 at 10:37 +0100, Jonny Grant wrote:
> Hello
> This isn't real code, it's just an example to ask this question:
> 
> Would GCC optimizer ever re-order these statements and cause a NULL ptr
> de-reference SIGSEGV? I recall reading a Chris Lattner paper indicating
> it could happen.
> 
> void f(int * p)
> {
>     if(!p)
>     {
>         return;
>     }
> 
>     printf("%d\n", *p);
> }

No it won't.

What Chris paper says is about something like:

void f(int * p)
{
    int x = *p;

    if(!p)
    {
         return;
    }

    printf("%d\n", x);
}

if p is NULL, this is an UB and the compiler can do anything.
-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[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