Created
February 10, 2016 18:28
-
-
Save benjholla/28db25b1f7ffac4eb98c to your computer and use it in GitHub Desktop.
An example of using long jumps in C to jump interprocedurally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <setjmp.h> | |
// saves the stack context/environment for nonlocal gotos | |
jmp_buf env; | |
// foo immediately makes a long jump to the setjmp in main | |
void foo(void){ | |
longjmp(env,1); | |
} | |
// prints "hello cruel world!" | |
int main(int argc, char *argv[]){ | |
printf("hello"); | |
// setjmp returns 0 if returning directly and nonzero | |
// when returning from a longjmp or siglongjmp using | |
// the saved stack context | |
if (!setjmp(env)){ | |
foo(); | |
printf(" sane world!\n"); | |
return 0; | |
} else { | |
printf(" cruel world!\n"); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment