Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ((void (*)())buf)(); mean?

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?

like image 992
sh.3.ll Avatar asked Jan 14 '20 13:01

sh.3.ll


People also ask

What void * means in C?

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.

What does void * func () mean?

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.


2 Answers

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.

like image 98
3 revs Avatar answered Sep 19 '22 14:09

3 revs


pointer buf is converted to the pointer to void function taking unspecified number of parameters and then dereferenced (ie function called).

like image 27
0___________ Avatar answered Sep 20 '22 14:09

0___________