I am solving a binary exploitation challenge on picoCTF and came across this piece of code:
((void (*)())buf)();
where buf
is a character array.
I solved the challenge but can't seem to understand what exactly it's doing. I looked at this thread but I couldn't make it out.
What does ((void (*)())buf)();
mean?
void (C++) If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword. A void* pointer can't be dereferenced unless it's cast to another type. A void* pointer can be converted into any other type of data pointer.
Void functions, also called nonvalue-returning functions, are used just like value-returning functions except void return types do not return a value when the function is executed. The void function accomplishes its task and then returns control to the caller. The void function call is a stand-alone statement.
void (*)()
is a type, the type being "pointer to function that takes indeterminate arguments and returns no value".
(void (*)())
is a type-cast to the above type.
(void (*)())buf
casts buf
to the above type.
((void (*)())buf)()
calls the function (passing no arguments).
In short: It tells the compiler to treat buf
as a pointer to a function, and to call that function.
pointer buf
is converted to the pointer to void function taking unspecified number of parameters and then dereferenced (ie function called).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With