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
12
votes
2 answers

Detect recursion robustly even in the presence of non-local jumps

I have a particular function (a signal handler) for which I'd like to detect recursion, i.e. to figure out if the function has directly or indirectly called itself. The tricky bit is that the function calls some code not under its control at one…
nneonneo
  • 171,345
  • 36
  • 312
  • 383
11
votes
6 answers

About setjmp/longjmp

I was investigating setjmp/longjmp and found out that setjmp saves registers such as instruction pointer, stack pointer etc... However what I don't get here is that, can't the data in the stack of the thread itself be modified between the call to…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
11
votes
2 answers

Is it okay to longjmp before calling va_end?

In this Q&A it is established that you should always call va_end(): What exactly is va_end for? Is it always necessary to call it? But what if a piece of code longjmp's before you reach the va_end? Is there any promise on va_end's part that it will…
10
votes
2 answers

longjmp and RAII

So I have a library (not written by me) which unfortunately uses abort() to deal with certain errors. At the application level, these errors are recoverable so I would like to handle them instead of the user seeing a crash. So I end up writing code…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
10
votes
2 answers

inconsistent warning: variable might be clobbered by ‘longjmp’ or ‘vfork’

I have mostly convinced myself that I have encountered some g++ 4.8.3 bug, but I thought I would ask this list first because I have very little experience with setjmp/longjmp. I have simplified my code in question to the following foo.cxx: #include…
boulderpika
  • 101
  • 1
  • 5
10
votes
4 answers

Exactly what "program state" does setjmp save?

I've read that setjmp "saves the program state" in the passed-in jmp_buf variable, but I haven't found any description of exactly what that entails. Does it make a copy of all the application's memory? Just the registers? The stack?
Norg74
  • 246
  • 4
  • 14
9
votes
2 answers

Lua coroutines -- setjmp longjmp clobbering?

In a blog post from not too long ago, Scott Vokes describes a technical problem associated to lua's implementation of coroutines using the C functions setjmp and longjmp: The main limitation of Lua coroutines is that, since they are implemented…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
9
votes
3 answers

Macro hell: Platform-independent pointer to setjmp/sigsetjmp

I am writing multiplatform code which needs to use a pointer to setjmp/sigsetjmp. Normally that would be as simple as doing #include void * sigsetjmp_p = sigsetjmp; However, ISO and POSIX state that setjmp/sigsetjmp can be defined as a…
fons
  • 4,905
  • 4
  • 29
  • 49
8
votes
1 answer

setjmp and longjmp - understanding with examples

I know the definition of setjmp and longjmp. setjmp stores the environment in stack context and the other one restores. But i think there is somewhere some lack of understanding in my part. Can someone explain me, with the help of good examples as…
RajSanpui
  • 11,556
  • 32
  • 79
  • 146
8
votes
1 answer

Do I have to call 'longjmp' after a 'setjmp'

In the manpage on longjmp and setjmp, there's this line: If the function which called setjmp() returns before longjmp() is called, the behaviour is undefined. Does that mean that I actually must call longjmp somewhere in the function that called…
Michail
  • 1,843
  • 1
  • 17
  • 21
8
votes
4 answers

Warning "might be clobbered" on C++ object with setjmp

#include #include int main(int argc, char**) { std::vector foo(argc); jmp_buf env; if (setjmp(env)) return 1; } Compiling the above code with GCC 4.4.1, g++ test.cc -Wextra -O1, gives this confusing…
Tronic
  • 10,250
  • 2
  • 41
  • 53
7
votes
6 answers

How does longjmp work?

I need to understand HOW longjmp function works; I know what it does, but I need to know how it does it. I tried to disas the code in gdb but I can't understand some steps. The code is: 0xb7ead420 : push %ebp 0xb7ead421…
Aslan986
  • 9,984
  • 11
  • 44
  • 75
7
votes
1 answer

What are the actual stuff in the jmp_buf when using setjmp and longjmp?

setjmp() is supposed to save registers including "return address" and "stack pointer" into "jmp_buf". When I compile (both gcc and clang) and debug the following program under x86_64 with glibc, I cannot understand what is in the "jmp_buf" and where…
WindChaser
  • 960
  • 1
  • 10
  • 30
7
votes
3 answers

excellent setjmp/longjmp tutorials

Hi I'd like to read good tutorials on setjmp/longjmp in C. It'd be better if there're examples which are real rather than artificial. Thanks.
Nyan
  • 2,360
  • 3
  • 25
  • 38
7
votes
1 answer

Why does setjmp(3) not save all registers on AMD64?

I was browsing through the source of various setjmp and longjmp implementations and noticed that not all of the CPU registers are saved in the jmp_buf structure. After reviewing the AMD64 ABI, I noticed that only the callee-saved registers are…
haste
  • 1,441
  • 1
  • 10
  • 21
1
2
3
11 12