In chapter 6.2.1 of Mitchell’s book (Concepts in Programming Languages), it mentioned that:
Type Casts. Type casts allow a value of one type to be used as another type. In C in particular, an integer can be cast to a function, allowing a jump to a location that does not contain the correct form of instructions to be a C function.
so I was up to use this non-safety and doing something unusual
I tried something like this (pseudo-code):
int x = 0;
print "loop";
x();
to create an infinite loop. I tried by changing and testing, but I couldn’t cope with that.
How can I do something or everything else like these stuffs?
Thanks in advance
@Soroush, here’s an example that might help you better understand what’s going on behind the scenes:
For me, it works on x86_64 when compiled with
clang -O0(well, it works until the stack is exhausted since this is infinite recursion and each function call chews through stack space).I determined the offset 22 by compiling, then disassembling and subtracting the address of the start of
main()from the address of the secondprintf().First, I compiled it:
Then disassembled it:
…which produced this output:
_main:indicates the entrypoint of themain()function, whose first address is 0x100000ee0. The firstcallqinstruction corresponds with the firstprintf()call, which I want to skip, so I chose the address just after that: 0x100000ef6. 0x100000ef6 minus 0x100000ee0 is 22 decimal.