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
0
votes
0 answers

`siglongjmp` to a returned function

My question arises from implementing a version of the standard I/O fgets function, called tfgets, that times out and returns NULL if it does not receive an input line on standard input within 5 seconds. A reference solution is: #include…
Sean
  • 1,055
  • 11
  • 10
0
votes
2 answers

Is there some version of longjmp that can output long values?

I'm working on a framework on C language, for it I want to implement exceptions, for it I'm using longjump with setjump, but on x64 machines longjump still outputs an integer. I've created a class (struct with vptr essentially), which represents…
0
votes
0 answers

stand alone explanation of sigsetjmp

I'd like to understand the usage of sigsetjmp(). But I am yet to find a standalone explanation. For example, the following URL mentions other functions like setjmp() and longjmp(), which I don't care. Consequently, I can not follow the explanation…
user1424739
  • 11,937
  • 17
  • 63
  • 152
0
votes
1 answer

C code setjmp declare functions?

I'm absolutely not a coder, but I'm trying to get a very old program to compile from fortran to C, so that I can play a game i've not played in 20+ years (originally created on HP3000 in the late 70s!) on my linux box. Someone has written a…
JamesT
  • 1
0
votes
0 answers

Passing more data than an 'int' between setjmp() + longjmp()

I am tinkering with setjmp and longjmp, implementing a simple try/catch exception system with them. setjmp will return the integer 0 upon first calling it, and returns any other value you pass in into the call to longjump otherwise. However,…
Qqwy
  • 5,214
  • 5
  • 42
  • 83
0
votes
0 answers

Alternative of sigsetjmp() in Shell

I want the work of the script below to continue after the interrupt arrives and until the first successful entry. In C usually used sigsetjmp() and siglonglmp(). Any alternatives in Shell scripts? Thanks. i() { echo "Okay ..." } trap 'i'…
0
votes
0 answers

Can I use a pointer to volatile to circumvent the indeterminate behavior of setjmp?

In the following code: int main(void) { jmp_buf buf; int x = 1; // Do stuff with x in a non-volatile, optimized way. volatile int* x_p = &x; switch (setjmp(buf)) { case 0: { *x_p = 3; longjmp(buf, 1); } case 1: { …
Luis
  • 1,210
  • 2
  • 11
  • 24
0
votes
1 answer

setjmp, longjump and stack reconstruction

Normally setjmp and longjmp does not care about call stack - instead functions are just preserving and restoring registers. I would like to use setjmp and longjmp so that call stack would be preserved, and then restored at different executing…
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
0
votes
0 answers

Gnu AS: Compilation error on instruction: far jmp 0x8, 0x80000

I have the following scenario in my code: .code16 // NOTE: current CS:IP address is 0x8000:0000 lgdt kernel_gdt_descriptor // Enable PE bit in cr0 register mov %cr0, %eax or %eax, $0x1 mov %eax, %cr0 ljmp 0x8, _pmode_entry _pmode_entry: jmp…
Maxim Blinov
  • 886
  • 9
  • 33
0
votes
0 answers

porting sigsetjmp / sigjmp_buf (in C) to windows

Is there a clean way (preferably compatible with mingw64) to port sigsetjmp / sigjmp_buf to windows?
mwag
  • 3,557
  • 31
  • 38
0
votes
1 answer

C: Passing Strings by reference?

I wrote the following code in order to understand setjmp and longjmp functions but I think the issue is unrelated to those functions. I am expecting the output to be: function1 function2 function2 but I keep…
Jenna Maiz
  • 792
  • 4
  • 17
  • 38
0
votes
1 answer

How to get rid of an error when quitting pthread while it's in sleep()?

first of all I'd like to apologize for the confusing title. But here's my question: I have a main function which spawns another thread which is only working from time to time with "sleep(3)" in between. Inside the main.c , I've a while loop which…
Marco Hegenberg
  • 612
  • 7
  • 5
0
votes
4 answers

How to return to main and not the function calling it?

I have a function() which calls anotherFunction(). Inside anotherFunction(), there is an if statement which, when satisfied returns back to main() and not to function(). How do you do this?
bee.
  • 11
0
votes
2 answers

How to save the state(context) of multiple functions in a program

We are trying to switch between multiple functions in our C program after saving the state using setjmp and longjmp but for only one function we are able to save the context not for other two functions. What could be the possible solution to it.…
Kush
  • 1
0
votes
1 answer

setjmp longjmp crash under Netbeans cygwin Windows XP

The following is an example code given at Purdue University CS class. I have done very little change to original, for debugging purposes. You can see the original code at https://www.cs.purdue.edu/homes/cs240/lectures/Lecture-19.pdf. The problem i…
nsamuel
  • 105
  • 1
  • 11
1 2 3
11
12