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

Why does this setjmp program print a 5?

The following code just ends up printing "5" #include #include static jmp_buf buf; float funcB() { setjmp(buf); return 1.6f; } int funcA() { longjmp(buf,5); std::cout<<"b"; return 2; } int main() { …
foips
  • 596
  • 5
  • 19
3
votes
1 answer

Is it possible to do a longjmp to a gcc pre_init function right before static variable initialisation?

To simulate a complete restart of an application (GNU C version 4.1.2 (arm-linux-gnueabi)) including static variable initialization I try to setjmp/longjmp to a gcc pre_init hook function on request. The longjmp in fact reaches the .preinit_array…
Marcel
  • 137
  • 1
  • 8
3
votes
1 answer

Are objects created before setjmp destructed?

In jpeglib, one has to use setjmp/longjmp to implement custom error handling. There are lots of resources where it is said that setjmp/longjmp do not play well with c++ (for example answers in this question tell they do go along with RAII), but the…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
3
votes
1 answer

Explicit call to destructor before longjmp/croak

I'm writing a PERL XS interface to a C++ library. I need to call croak when the library throws an exception. Doing it directly in the exception handler misses the call to the caught exception's destructor, as expected of a longjmp call. This is…
Irfy
  • 9,323
  • 1
  • 45
  • 67
3
votes
1 answer

Why was ucontext added to and then removed from POSIX?

As far as I know, ucontext offers something better then setjmp. But it's deprecated and now removed from the POSIX spec. So why did it appear and why was it removed?
eonil
  • 83,476
  • 81
  • 317
  • 516
3
votes
5 answers

How is C++ try/catch different than C setjmp/longjmp?

I was aware of exception handling in C++ using the try and catch blocks. I wondered if this functionality was there in C. So, now I know that basic error handling in C is done by setjmp/longjmp. Since setjmp/longjmp isn't present in C++ , Can one…
sudeepdino008
  • 3,194
  • 5
  • 39
  • 73
2
votes
2 answers

Why does setjmp traditionally save registers?

Just consider i386 as an example, but an analogous question applies to other archs. The traditional i386 jmp_buf saved by setjmp consists of 6 saved registers: ebx, esi, edi, ebp, esp, and eip. Of these, the first 4 are caller-saved per the ABI, so…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
2
votes
1 answer

Using the setjmp and longjmp

Having such a simple C code #include #include void Com_Error(int); jmp_buf abortframe; int main() { if (setjmp (abortframe)){ printf("abortframe!\n"); return 0; } …
darro911
  • 136
  • 6
2
votes
1 answer

Why isn't setjmp saving the stack?

Why isn't setjmp saving the stack? Consider the following code: #include jmp_buf Buf; jmp_buf Buf2; void MyFunction() { for(int i = 0; i < 5; i++) { std::cout << i << std::endl; if(!setjmp(Buf)) …
Daniel
  • 30,896
  • 18
  • 85
  • 139
2
votes
2 answers

C "error: longjmp causes uninitialized stackframe" when using longjmp

I'm trying to build a simple cooperative multithreading library in C. Basically, it is possible to create threads using thread_create, add them to the runqueue with thread_queue, and then execute them to completion using thread_exec. Within the…
georgijs_
  • 45
  • 7
2
votes
0 answers

Longjmp failure with emscripten-fastcomp

I was trying to use setjmp/longjmp on a C++ project compiled with emscripten (version 1.39.16-fastcomp, can't use WebAssembly), and I came across instances of the program crashing after longjmp. I tried compiling in WebAssembly just for reference…
teehessar
  • 21
  • 3
2
votes
1 answer

linux C languang. setjmp longjmp alarm

execute my code jmp_buf a; void sig_handler(int signum) { if(signum == SIGINT) exit(0); printf("In the handler\n"); sleep(2); alarm(3); longjmp(a,1); } int main(int argc, char *argv[]) { int j=0; …
hkjaaaip
  • 63
  • 1
  • 1
  • 5
2
votes
1 answer

What's the function of alloca() with setjmp?

This question comes from Practical usage of setjmp and longjmp in C and How to implement coroutine within for loop in c which I asked. jmp_buf bufferA, bufferB; void routineB(); // forward declaration void routineA() { int r = 0; …
JustWe
  • 4,250
  • 3
  • 39
  • 90
2
votes
1 answer

can't get alarm() to work more than twice

static void AlarmHandler(int sig) ; int i=0; jmp_buf mark; int main(int argc, char * argv[]){ setjmp(mark); signal(SIGALRM, AlarmHandler); alarm(2); while(1); return 0; } static void AlarmHandler(int sig) { signal(SIGALRM, SIG_IGN); …
2
votes
2 answers

Legal uses of setjmp and GCC

Using GCC (4.0 for me), is this legal: if(__builtin_expect(setjmp(buf) != 0, 1)) { // handle error } else { // do action } I found a discussion saying it caused a problem for GCC back in 2003, but I would imagine that they would…
Chris Lutz
  • 73,191
  • 16
  • 130
  • 183