The whole program has undefined behavior.
setjmp(b);
stores the stack state.jump()
is called.- `setjmp(a);' stores the stack state again.
longjmp(b, 1);
restores the stack to the point beforejump()
was ever called. So the state stored ina
is now invalid.- Execution continues at the
if
inmain()
. longjmp(a, 1);
is called. Ouch. This causes undefined behavior due to 4 above.
Your confusion probably results from the slightly imprecise use of the world "return" in the Linux docs for setjmp()
.
The stack context will be invalidated if the function which called
setjmp()
returns.
In your example, the function jump()
didn't return in the normal way, but the effect was the same: the stack was "chopped" by the first longjmp()
to the state before jump()
, which is what a return does, too.