Skip to content

Instantly share code, notes, and snippets.

@xxuejie
Created December 7, 2012 22:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxuejie/4237169 to your computer and use it in GitHub Desktop.
Save xxuejie/4237169 to your computer and use it in GitHub Desktop.
setjmp does not restore local variables
$ clang setjmp-test.c
$ ./a.out
setjmp normal execution path, level: 0, prev_jmp: 0x0
setjmp normal execution path, level: 1, prev_jmp: 0x7fff5df345f0
level is 2, perform longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x7fff5df345f0
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 0, prev_jmp: 0x0
Exiting setjmp function, level: 0, prev_jmp: 0x0
$ emcc setjmp-test.c
$ node a.out.js
setjmp normal execution path, level: 0, prev_jmp: (nil)
setjmp normal execution path, level: 1, prev_jmp: 0x14
level is 2, perform longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
setjmp exception execution path, level: 1, prev_jmp: 0x14
prev_jmp is not empty, continue with longjmp!
(this is an infinite loop)
#include <setjmp.h>
#include <stdio.h>
typedef struct {
jmp_buf* jmp;
} jmp_state;
void setjmp_func(jmp_state* s, int level) {
jmp_buf* prev_jmp = s->jmp;
jmp_buf c_jmp;
if (level == 2) {
printf("level is 2, perform longjmp!\n");
longjmp(*(s->jmp), 1);
}
if (setjmp(c_jmp) == 0) {
printf("setjmp normal execution path, level: %d, prev_jmp: %p\n", level, prev_jmp);
s->jmp = &c_jmp;
setjmp_func(s, level + 1);
} else {
printf("setjmp exception execution path, level: %d, prev_jmp: %p\n", level, prev_jmp);
if (prev_jmp) {
printf("prev_jmp is not empty, continue with longjmp!\n");
s->jmp = prev_jmp;
longjmp(*(s->jmp), 1);
}
}
printf("Exiting setjmp function, level: %d, prev_jmp: %p\n", level, prev_jmp);
}
int main(int argc, char *argv[]) {
jmp_state s;
s.jmp = NULL;
setjmp_func(&s, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment