[net.unix-wizards] Steve Rosen's setjmp problem

ark@alice.UUCP (Andrew Koenig) (06/07/85)

Steve Rosen complains that the following C program fails:

#include <setjmp.h>

static jmp_buf env;
int mode;

main(){
	foo();
	longjmp(env, 1);
}

foo(){

  mode = setjmp(env);
  if (mode != 0) magic();
}

magic()
{
	printf("HERE I AM\n");
	exit(0);
}

The reason the program fails is that foo returns after calling
setjmp.  This destroys the environment that setjmp saved,
so there is nothing for longjmp to return to.  General rule:
every call to longjmp must be made from a function that
is a dynamic descendent of the one that made the corresponding
call to setjmp.