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
6
votes
3 answers

Does this code contain a hidden bug?

The following code : Runs fine when compiled with gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5/32bits) Runs fine when compiled with MSVC10 (Win7/32bits) Crashes when running with gcc version 4.5.2 (MinGW on Win7/32bits) main.cpp : # include…
overcoder
  • 1,523
  • 14
  • 24
6
votes
3 answers

Use of setjmp and longjmp in C when linking to C++ libraries

I would like to use setjmp and longjmp in a C program that links to a library that is implemented in C++ (but has a C API). The C++ code does do dynamic memory allocation and pointers get passed through the API, but as long as the C side of the code…
Posco Grubb
  • 522
  • 2
  • 7
  • 20
6
votes
2 answers

Implementing setjmp and longjmp in C without built in functions or assembly (getting incorrect return values)

I'm trying to test 2 of my functions that sort of mimic setjmp and longjmp for a homework - which is pretty difficult since we're not allowed to use built in functions or assembly asm() to implement the longjmp and setjmp functions. (Yes, that's…
adventuredoge
  • 345
  • 5
  • 17
6
votes
4 answers

How to insulate a job/thread from crashes

I'm working on a library where I'm farming various tasks out to some third-party libraries that do some relatively sketchy or dangerous platform-specific work. (In specific, I'm writing a mathematical function parser that calls JIT-compilers, like…
Charles Pence
  • 300
  • 3
  • 13
6
votes
1 answer

Recursive coroutines in C (C99)

While implementing a communication protocol, we have an encoder that traverses some structs recursively and encodes them into a binary message. So far so good, but now the buffer has to split out into multiple chunks of fixed size, e.g. the upper…
Stasik
  • 2,568
  • 1
  • 25
  • 44
6
votes
2 answers

Safe usage of `setjmp` and `longjmp`

I know people always say don't use longjmp, it's evil, it's dangerous. But I think it can be useful for exiting deep recursions/nested function calls. Is a single longjmp faster than a lot of repeated checks and returns like if(returnVal != SUCCESS)…
cshu
  • 5,654
  • 28
  • 44
6
votes
1 answer

Where is glibc's code for setjmp?

I was interested in what exactly setjmp does at least in x86_64 linux, so I searched through glibc's source code, but I cannot really find where the register saving is done. Could you explain what is going on here? setjmp.h extern int _setjmp…
user3810155
6
votes
3 answers

trapping signals in a multithreaded environment

I have a large program that needs to be made as resilient as possible, and has a large number of threads. I need to catch all signals SIGBUS SIGSEGV, and re-initialize the problem thread if necessary, or disable the thread to continue with reduced…
camelccc
  • 2,847
  • 8
  • 26
  • 52
6
votes
2 answers

How to safely get the return value of setjmp

I would like to return an error code using longjmp, and pass it on from the function that called setjmp. Simplified code: int do_things(stuff ........) { int error_code; jmp_buf jb; if ((error_code = setjmp(jb)) == 0) { /* do stuff */ …
Gábor Buella
  • 1,840
  • 14
  • 22
6
votes
7 answers

longjmp() from signal handler

I'm using the following code to try to read an input from user and timeout and exit if more than 5 seconds pass. This is accomplished through a combination of setjmp/longjmp and the SIGALRM signal. Here's the code: #include #include…
sttwister
  • 2,279
  • 1
  • 19
  • 23
5
votes
3 answers

Why does the setjmp/longjmp crash on MSVC when it didn't in MinGW?

I wanna use setjmp()/longjmp() to implement a coroutine system. Then I decide to code a little .c file to test it. In MinGW, it's OK; I got the result I want. But when I compile it in MSVC++, the program crashes: "access violation" #include…
TZW
  • 51
  • 1
  • 2
5
votes
3 answers

setjmp and omit frame pointer

I've been trying to track down an intermittent crashing bug in my code (which uses setjmp), and narrowed it down to: shows up when compiling with /O2, goes away with /O2 /Oy-, i.e. only shows up with omit frame…
rwallace
  • 31,405
  • 40
  • 123
  • 242
5
votes
2 answers

Hiding longjmps in C++ interface to C code

What would be the right way to generate a C++ API for old C code that is extensively using longjmp with multiple jump targets for error management? My idea was to write a function that sets jump targets for each used target, e.g.: void catchJumps()…
urzeit
  • 2,863
  • 20
  • 36
5
votes
1 answer

C++ and C library using longjmp

I'm working with Lua, which has a C API and its error raising functions use longjmps. When raising an error I first build a message describing what went wrong and then tell Lua to raise the error. For example std::stringstream ss; ss << "'" <<…
user1520427
  • 1,345
  • 1
  • 15
  • 27
4
votes
1 answer

How do I ensure the `SIGINT` signal handler is called as many times as `Ctrl+C` is pressed (with `longjmp`)?

Setup In the code below, which simply prints some text until it times out, I added a handler (onintr()) for SIGINT. The handler onintr() does the following: Resets itself as the default handler. Prints out some text. Calls longjmp(). Issue It…
gomfy
  • 637
  • 8
  • 16
1 2
3
11 12