C Programming - Skip

This will give desired output for Windows/Linux x86 Platform with gcc and visual studio 6.0 compilers

int f()
{
int i;
int *j = &i;
*(j+2) += 7;
}

int main()
{
int i=10;
f();
i = 20;
printf("i=%d\n",i);
}

Here the function f() manupulates the return address i.e. where it was supposed to return to main() because the return address(EIP) is pushed, during a function call, into the stack. We computed that, to skip the instruction i=20, we need to increment the EIP by 7 such that the resulting address will point to printf statement.

Labels:


About this entry