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

Performance overhead of using volatile for setjmp/longjmp

For setjmp/longjmp to work, you need to declare local variables as volatile. If someone is compiling its code with -O3, how much would be the impact of volatile variables on performance . Would it be huge or only a tiny bit on an x86 multicore…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
3
votes
1 answer

Using fork with setjmp/longjmp

I was trying to implement a checkpointing scheme based on multithreaded fork using fork combined with setjmp/longjmp. I was hoping my solution would work but as expected it didn't. The code is shown below with an example usage for…
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
3
votes
1 answer

Why isn't setjmp in the std namespace when including ?

The title say it all. After including , longjmp and jmp_buf are in the std namespace, but setjmpis not. I verified this on MinGW4.5 and MSVC10. After looking into the header, I could not figure out the reason of this choice. I was…
overcoder
  • 1,523
  • 14
  • 24
3
votes
3 answers

Special treatment of setjmp/longjmp by compilers

In Why volatile works for setjmp/longjmp, user greggo comments: Actually modern C compilers do need to know that setjmp is a special case, since there are, in general, optimizations where the change of flow caused by setjmp could badly corrupt…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
3
votes
3 answers

C++: will an std::runtime_error object leak in a longjmp?

Suppose I have some C++ code which has a try-catch block in which the catch part will trigger a long jump: #include #include #include void my_fun() { jmp_buf jump_buffer; if (setjmp(jump_buffer)) …
anymous.asker
  • 1,179
  • 9
  • 14
3
votes
2 answers

Why is this usage of setjmp/longjmp undefined behavior?

The code #include template void create_checkpoint(std::jmp_buf buf, Callable&& callable) { if (setjmp(buf) != 0) { callable(); } } #include struct announcer { int id; …
Incomputable
  • 2,188
  • 1
  • 20
  • 40
3
votes
1 answer

Using setjmp() and longjmp() to prevent segmentation fault in a program

I have written a program to prevent segfault using setjmp() and longjmp(), but the program that I have written prevents segfault from happening only one time (I'm running my code inside a while loop). Here is my code: #include #include…
Prateek Joshi
  • 3,929
  • 3
  • 41
  • 51
3
votes
1 answer

SIGSEGV cannot be caught twice with sigaction

The following code has segmentation fault at the second printf, while this is expected to be handled (setjmp). Note that each printf create segmentation fault due to wrong format string. The first segmentation fault is handled properly but not the…
3
votes
1 answer

MSVC warning 4611 regarding setjmp w/POD struct

Trying to turn up some warning levels on a C codebase that also builds as C++. I'm giving Visual Studio a shot (for some reason). Got a warning about setjmp interactions, despite not seeing any relevant destructors. so I did a test: #include…
3
votes
1 answer

Safe usage of longjmp/setjmp with volatile

I consider to use a TRY/CATCH macro based on setjmp/longjmp for error handling. Otherwise some of my quite structued functions will be blown up by ugly if statements and loop flags. The code is like this example: int trycatchtest(int i) { int…
MichaelW
  • 1,328
  • 1
  • 15
  • 32
3
votes
1 answer

setjmp/longjmp between threads to handle timeout

I'm porting a software from an embedded computer to a Linux machine. (Ubuntu 14.04 or Raspbian (raspberry pi)) The original program was using setjmp/longjmp to handle timeout and CTRL+C event. It was running on a Microcontroller with a single main…
ssinfod
  • 975
  • 2
  • 15
  • 31
3
votes
2 answers

In C: sending func pointers, calling the func with it, playing with EIP, jmp_buf and longjmp

I need to make sure i understand some basic stuff first: how do i pass function A as a parameter to function B? how do i call function A from inside B ? Now for the big whammy: I'm trying to do something along the lines of this: jmp_buf…
Yonatan
  • 93
  • 3
  • 6
3
votes
1 answer

What is stack reallocation and when does it happen?

It is stated that you stack reallocation can happen. I don't understand this. I thought the whole point of setjmp/longjmp was to save the stack, and that it would be valid when longjmp'ing back. The comment seems to suggest the whole stack could be…
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
3
votes
1 answer

C++ and a safe way to jump out of dynamically generated code

My project is written in C++, and it makes use of dynamically generated code to glue some things together (using Fabrice Bellard's TCC and a bit of manually generated assembly thunks). Dynamically generated code sometimes jumps into "runtime…
carsten
  • 163
  • 6
3
votes
1 answer

Implementing preemptive microthreads using signal handlers and setjmp/longjmp

I want to implement POSIX compliant microthreads in Linux environment. Basic idea is as follows: Using technique described here, assign new stack space for each fiber. Using setitimer, create timer that will send signals in constant time interval.…
monkey
  • 103
  • 1
  • 6