GCC optimization re-order statements

Jonny Grant jg@jguk.org
Fri Jun 11 09:37:18 GMT 2021


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);
}

I which case, a lot of production code faces issues, must be changed to:

void f(int * p)
{
    if(p)
    {
        printf("%d\n", *p);
    }
}

Jonny


More information about the Gcc-help mailing list