In C: sending func pointers, calling func with it, playing with EIP, jmp_buf and longjmp

I need to make sure that I understand some basic things first:

  • How to pass function A as a parameter to function B?
  • How do I call function A from inside B?

Now for the big whammy:

I am trying to do something like that:

jmp_buf buf;
buf.__jmpbuf[JB_PC] = functionA;
longjmp(buf,10);

The value that I want to use longjmpto go to the function. How should I do it?

+3
source share
2 answers

You need to use a function pointer. Function pointer declaration syntax:

rettype (*)(paramtype1,paramtype2,...,paramtypeN)

So, for example, we might have the following code:

char functionA(int x)
{
      printf("%d\n",x):
      return 'a';
}

char functionB(char (*f)(int), int val)
{
       return f(val); // invokes the function pointer
}

int main(int argc, char* argv[])
{
       char result = functionB(&functionA,3); // prints "3"
       printf("%c\n",result); // prints 'a'
       return 0;
}

, , & functionA A, ... , , , . , , .

, , , , -, . setjmp , longjmp, , . jmp_buf, , . , (, , setjmp ), setjmp.h jmp_buf. , , jmp_buf , .

+3
  • A B:

    typedef void function_type (void);

    void functionA (void) {   printf ( " A\n" ); }

    int main (int argc, char ** argv) {   functionB (& functionA);   return (0); }

  • A B:

    void functionB (function_type * func) {   FUNC(); }

  • longjmp() . : " " - . , ?

0

Source: https://habr.com/ru/post/1745942/


All Articles