GCC optimization re-order statements
Xi Ruoyao
xry111@mengyan1223.wang
Fri Jun 11 09:44:11 GMT 2021
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@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list