Reputation: 21
As extra credit for a homework assignment I'm working on, we have to try and call win()
by providing input to the executable of the below code (we can't change the code). We have to provide a number n
and a value for target
, which is then executed. I know what the value of n
is. I'm not super well-versed in C, but I was looking through many other questions on here about using function pointers, and I thought passing in &win
would work, but that just prints Third challenge
an excessive amount of times and then seg faults. Can someone point me in the right direction, or to more resources so I could better understand how the function pointer is working in this program?
typedef void (*funcptr_t)();
int win()
{
printf("Great Job\n");
exit(0);
}
int main()
{
int n = 0;
unsigned long long target;
printf("Third challenge\n");
scanf("%d", &n);
scanf("%llu", &target);
if (n == 0xc0decafe) {
funcptr_t funcptr = (funcptr_t)target;
funcptr();
}
}
Upvotes: 0
Views: 399
Reputation: 78683
If, in advance, you could somehow work out what address the win
function will have at runtime, then you can provide that address as the target
input value. Then the code will be able to make a function pointer out of that value, which will correspond to the actual win
function, and then invoke that function.
And the tool to help you get the address associated with a given function is typically the linker, or some kind of object/executable dump utility.
See How does the linker find the main function? for an overview of the tools.
Upvotes: 1