Questions tagged [setjmp]

Anything related to the setjmp() and longjmp() routines in the C standard library (provided by the setjmp.h header file), which provide control flow that modify the usual call and return sequence of a subroutine.

The setjmp and longjmp pair of functions allow for the creation of non-local jumping in C programs—it is essentially a non-local goto that can jump across functions. This is a feature which is basically equivalent to exceptions in later languages, and has actually been used to write exception-like systems for C.

It works by loading the environmental state (jmp_buf) saved by an earlier setjmp over the current state. In the process, it also returns a value at the site of the original setjmp call. In practice, the environmental state is tied in with the stack, and this can cause some trouble.

For example, if the frame in which setjmp returns, then jumping to that particular frame invokes undefined behavior because that frame no longer exists. Another consequence of longjmp is that no stack unwinding occurs, so open files will not be closed and heap variables will not be freed.

In most C standard libraries, it can be found under setjmp.h. It has the call signature:

void longjmp(jmp_buf env, int val);

If you do not understand any of the above, then please do not use longjmp! It is very much a tool that can bite you if you are inexperienced. This is especially true for C++, which has exceptions which are harder to abuse than longjmp is.

175 questions
4
votes
3 answers

Using setjmp and longjmp with a local jmp_buf

In the case that a local jmp_buf is actually represented by registers rather than stack memory, is it possible for setjmp or longjmp to cause the contents of the local jmp_buf to be indeterminate when setjmp returns from a longjmp? The suggested…
jxh
  • 69,070
  • 8
  • 110
  • 193
4
votes
2 answers

Why does setjmp/longjmp

I want to use setjmp/longjmp to reuse some code inside the main function (NOTE: this is only an exercise and not something I ever seriously plan on doing in the real world). The following code is what I've came up with: #include #include…
Ihato
  • 55
  • 3
4
votes
1 answer

Is it allowed to do longjmp() multiple times for one setjmp() call?

In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call. Is it safely allowed to do longjmp()…
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
4
votes
6 answers

Is longjmp supposed to restore the stack?

From what I understood, setjmp saves the current context and it's supposed to restore it when calling longjmp. However the next piece of code prints 15 (I compiled with -g and without any optimization). Did I misunderstand this construct or am I…
user2717954
  • 1,822
  • 2
  • 17
  • 28
4
votes
1 answer

In C, is there any way to clean up if a longjmp "passes through" my function?

I have the following call stack: library_function_1 ====> my_function ====> library_function_2 calls calls In C++, if library_function_2 throws an exception and library_function_1 catches it, my_function can safely…
user200783
  • 13,722
  • 12
  • 69
  • 135
4
votes
3 answers

C setjmp.h and ucontext.h, which is better?

Hi I'm need to jump from a place to another... But I would like to know which is better to use, setjmp or ucontext, things like: Are setjmp and ucontext portable? My code is thread safe using these library? Why use one instead another? Which is…
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
4
votes
0 answers

Communication protocol and local loopback using setjmp / longjmp

I've coded some relatively simple communication protocol using shared memory, and shared mutexes. But then I wanted to expand support to communicate between two .dll's having different run-time in use. It's quite obvious that if you have some…
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
4
votes
4 answers

Save & Copy jmp_buf C++

Is it possible and valide to make a copy of jmp_buf and restore it later? something like jmp_buf oldEnv = env; int val = setjmp(env); ....... env = oldEnv; I have used memcopy() and sizeof(env), to copy jmp_buf data. This seems to work fine. is…
lufthansa747
  • 1,923
  • 3
  • 26
  • 39
4
votes
1 answer

C++11 lambdas capturing by reference trivially destructible

I would like to know if the following leaks memory or not (specified by the standard) ... jmp_buf env; if(setjmp(env) == 0) { auto lambda = [&] () { ... longjmp(env, 1); }; lambda(); } which boils down to whether…
iggy
  • 1,613
  • 1
  • 18
  • 35
4
votes
1 answer

Deep stack unwinding

First of all, this is definitely about C, no C++ solutions are requested. Target: Return to the caller function (A) beyond multiple stack frames. I have some solutions, but none of them feels like the best option. The easiest one in the sense of…
4
votes
1 answer

stack unwinding from middle of stack

I'm learning gdb debugger and one question I cannot answer is: new function calls result in additional stack frames allocated for them and call stack grows downward; stack frame is deallocated and returned to unused memory. is it possible that a…
J.Zhuo
  • 43
  • 3
4
votes
1 answer

How does Non - local Jumps in C defined in setjmp.h work?

The C Reference Manual, Appendix B describes two functions setjmp and longjmp for something called non-local jumps. Apart from the basic understanding that setjmp saves the state information and longjmp restores the state, I haven't been able to…
Tapan Nallan
  • 1,762
  • 3
  • 17
  • 37
4
votes
3 answers

pthreads, setjmp, longjmp. How can you tell when a function is finished running?

I am writing a user space thread library. I have a struct that manages each thread. My threads are very simple, they take a function ptr and its arguments, and just run that function one time. Each thread has a jmp_buf and I use setjmp and longjmp…
what
  • 41
  • 2
4
votes
3 answers

How to do lua_pushstring and avoiding an out of memory setjmp exception

Sometimes, I want to use lua_pushstring in places after I allocated some resources which I would need to cleanup in case of failure. However, as the documentation seems to imply, lua_push* functions can always end up with an out of memory exception.…
E. T.
  • 847
  • 10
  • 26
3
votes
1 answer

use of Sys::SigAction::timeout_call unsafe?

I've just read Leon Timmermans' article What you should know about signal based timeouts and I was wondering how it/if it applies to the use of Sys::SigAction::timeout_call(). 1) First of all, it seems that timeout_call() uses longjmp and unsafe…
ErikR
  • 51,541
  • 9
  • 73
  • 124