int main(int argc, char *argv[])
{
*(int *) 0;
return 0;
}
Before you compile and run this, take a guess at what it will do first.
>>53368328
uhhh. NULL?
segfault?
>>53368347
>>53368365
Now compile and run it to see what actually happens.
>>53368386
pretty neat. but i'm not that deep into C to understand.
all it did was move some shit around00000000004004ed <main>:
int main(int argc, char *argv[])
{
4004ed: 55 push %rbp
4004ee: 48 89 e5 mov %rsp,%rbp
4004f1: 89 7d fc mov %edi,-0x4(%rbp)
4004f4: 48 89 75 f0 mov %rsi,-0x10(%rbp)
*(int *) 0;
return 0;
4004f8: b8 00 00 00 00 mov $0x0,%eax
}
4004fd: 5d pop %rbp
4004fe: c3 retq
4004ff: 90 nop
>>53368386
Explain what it does op.
>>53368328
what does this do? Move the memory address of the primitive 0?
>>53368328
I thought the image said "reptiles only" nm.
>>53368328
It's undefined behavior. It could do anything depending on the compiler, version of it and compile flags.
>>53368507
>compile it
>run it
>it just returns zero
seems pretty well defined to me
>>53368328
Probably nothing. Compiler will notice we do nothing with the value and won't even compile that line.
Aren't you trying to reference a null pointer that way?
>>53368527
Probably because your compiler removed that part. It would be perfectly acceptable if it crashed instead.
It's pretty similar to this: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html
$ gcc main.c -o main -Wall
main.c: In function ‘main’:
main.c:3:2: warning: statement with no effect [-Wunused-value]
*(int *) 0;
^
$
Unlock a terrorist iphone and simultaneously swap the content of 2 registers.
>pic related. My serious face.
>>53368328alexis@VMCS-V5:/tmp$ gcc -g -Wall main.c
main.c: In function ‘main’:
main.c:3:5: warning: statement with no effect [-Wunused-value]
*(int *) 0;
^(gdb) disass main
Dump of assembler code for function main:
0x00000000004004a6 <+0>: push %rbp
0x00000000004004a7 <+1>: mov %rsp,%rbp
0x00000000004004aa <+4>: mov %edi,-0x4(%rbp)
0x00000000004004ad <+7>: mov %rsi,-0x10(%rbp)
0x00000000004004b1 <+11>: mov $0x0,%eax
0x00000000004004b6 <+16>: pop %rbp
0x00000000004004b7 <+17>: retq
End of assembler dump.
(gdb)