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.
175
questions
1
vote
0
answers
49
views
Common Lisp CFFI setjmp/longjmp
I am calling functions in the MuPDF C library using Common Lisp and CFFI. MuPDF uses setjmp/longjmp for exception handling. The MuPDF website states:
All functions you call should be guarded by a ...
1
vote
1
answer
53
views
How to know if my signal handler is interrupting an async-signal-unsafe function?
According to this answer, I can only longjmp() out of a signal handler if it is not calling an async-signal-unsafe function. Is there a reliable way to know, from inside the signal handler, if the ...
1
vote
0
answers
32
views
Why can't I use value = = setjmp(env); [duplicate]
Recently while reading csapp, I find in nonlocal jump, I can't use
int value;
value = setjmp(env);
but I can use this
if(setjmp(env)){
// code
}
Why?
int value;
value = setjmp(env);
4
votes
1
answer
580
views
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.
...
2
votes
1
answer
75
views
Using the setjmp and longjmp
Having such a simple C code
#include <stdio.h>
#include <setjmp.h>
void Com_Error(int);
jmp_buf abortframe;
int main() {
if (setjmp (abortframe)){
printf("...
3
votes
3
answers
615
views
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 ...
1
vote
2
answers
98
views
Does setjmp literally save the state of the program?
So, if I were to malloc 500 bytes, then use setjmp to save the state, then free the 500 bytes, then longjmp, would I be able to access those 500 bytes? For example:
#include <stdio.h>
#include &...
4
votes
3
answers
646
views
Using setjmp and longjmp with a local jmp_buf
In the case that a local jmp_buf is actually represented by registers rather than stack memory, is it possible for setjmp or longjmp to cause the contents of the local jmp_buf to be indeterminate when ...
0
votes
1
answer
123
views
setjmp / longjmp does not jump where I think it should
I would like to understand how setjmp / longjmp works, so I created an example program, where routineA prints even, routineB prints odd numbers and they jump to each other with longjmp:
#include <...
-1
votes
1
answer
138
views
Creating a loop using setJmp longJmp
I am trying to create an infinite loop using those functions (unsuccessfully).
My code :
jmp_buf buf1;
void foo(){
int z = 4, y = 1;
int v = setjmp(buf1);
if(v == 0){
printf("A%...
2
votes
2
answers
902
views
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 ...
0
votes
1
answer
130
views
alternative to mangling jmp_buf in c for a context switch
In setjmp.h library in linux system jmp_buf is encrypted to decrypt it we use mangle function
*/static long int i64_ptr_mangle(long int p) {
long int ret;
asm(" mov %1, %%rax;\n"
...
3
votes
3
answers
161
views
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 <stdexcept>
#include <stdio.h>
#include <setjmp.h>
void my_fun()
{...
0
votes
1
answer
63
views
setjmp/longjmp and the state of open files
The C standard says that
All accessible objects have values, and all other components of the abstract machine218)
have state, as of the time the longjmp function was called, ...
4
votes
2
answers
530
views
Why does setjmp/longjmp
I want to use setjmp/longjmp to reuse some code inside the main function (NOTE: this is only an exercise and not something I ever seriously plan on doing in the real world).
The following code is what ...
2
votes
0
answers
203
views
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 ...
0
votes
1
answer
56
views
call empty function with address of local variable before setjmp, what for?
I read code of C library and can not understand what is going on:
struct Foo *foo = NULL;
lib_var((void *)&foo);
if (setjmp(get_jmp_buf()) == 0) {
foo = ...;
// other calculation that may ...
0
votes
1
answer
213
views
Assembly early return on a recursive function
This is more an academic exercise than anything else, but I'm looking to write a recursive function in assembly, that, if it receives and "interrupt signal" it returns to the main function, ...
-1
votes
1
answer
168
views
How to resolve MISRA C:2012 Rule 21.4?
I am using setjmp and longjmp in our code with standard library [#include <setjmp.h>]. When I am trying to use it showing following MISRA error: Violation of MISRA C:2012 21.4: The standard ...
1
vote
0
answers
130
views
setjmp and longjmp implementation in mmix
I've written an implementation of setjmp and longjmp in MMIX (assuming no name mangling).
I also hand-assembled it.
Are there any mistakes anyone can spot?
// Memory stack pointer is stored in $...
0
votes
1
answer
190
views
C difference between main thread and other threads
Is there a difference between the first thread and other threads created during runtime. Because I have a program where to abort longjmp is used and a thread should be able to terminate the program (...
0
votes
1
answer
285
views
custom setjmp/longjmp implemetation segmentation fault
I'm trying to implement custom setjmp/longjmp for x64 windows.
I have following code:
contextSave:
xorq %rdx,%rdx
movq %rdx,(%rcx)
movq %rbx,0x8(%rcx)
leaq 0x8(%rsp),%rax
movq %rax,0x10(%rcx) ...
0
votes
1
answer
533
views
How to use longjmp from main function to other functions?
#include <setjmp.h>
#include <stdio.h>
jmp_buf jmp;
int test() {
setjmp(jmp);
puts("Birds are crying");
return 0;
}
int main() {
longjmp(jmp,1);
return 0;
}...
4
votes
1
answer
1k
views
Is it allowed to do longjmp() multiple times for one setjmp() call?
In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp(...
0
votes
0
answers
222
views
`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 ...
2
votes
1
answer
288
views
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 ...
1
vote
1
answer
478
views
Why do I get a segmentation fault in this code with a signal handler?
I write a signal handler in my program, but in this file structure I get a segmentation fault. But if everything is all in one file, then everything works correctly. As I understand it, it can’t ...
1
vote
1
answer
262
views
problem with setjmp and longjmp to switch between 2 functions
I am trying to implement a code which continuously switched between functions fun() and main() which do nothing but to print on screen infinitely. I am trying to switch by setjmp and longjmp and using ...
3
votes
2
answers
709
views
Why is this usage of setjmp/longjmp undefined behavior?
The code
#include <csetjmp>
template <typename Callable>
void create_checkpoint(std::jmp_buf buf, Callable&& callable)
{
if (setjmp(buf) != 0)
{
callable();
}
...
4
votes
6
answers
2k
views
Is longjmp supposed to restore the stack?
From what I understood, setjmp saves the current context and it's supposed to restore it when calling longjmp. However the next piece of code prints 15 (I compiled with -g and without any optimization)...
3
votes
1
answer
1k
views
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). ...
4
votes
1
answer
316
views
In C, is there any way to clean up if a longjmp "passes through" my function?
I have the following call stack:
library_function_1 ====> my_function ====> library_function_2
calls calls
In C++, if library_function_2 throws an exception and ...
0
votes
2
answers
199
views
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 (...
1
vote
1
answer
249
views
When `volatile` is needed when `longjmp()` is called?
I am trying to make a minimal working example to show when volatile is needed. But the following example does not require the volatile. Could anybody show an example? Thanks.
#include <stdio.h>
...
0
votes
0
answers
140
views
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. ...
3
votes
1
answer
352
views
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 ...
1
vote
1
answer
377
views
Why a segmentation fault occurs calling a function inside setjmp()?
I do not understand why in the function middleFunc(), a segmentation fault is raisen when entry_point(arg) is invoked inside the if ( setjmp(middle) ) statement.
#include <stdio.h>
#...
0
votes
1
answer
263
views
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!)...
2
votes
1
answer
277
views
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 ...
0
votes
0
answers
153
views
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 ...
3
votes
1
answer
245
views
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 ...
1
vote
2
answers
206
views
What software-visible processor state needs to go in a jmp_buf on an x86-64 processor?
As stated, what software-visible processor state needs to go in a jmp_buf on an x86-64 processor when setjmp(jmp_buf env) is called? What processor state does not?
I have been reading a lot about ...
8
votes
1
answer
573
views
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 ...
0
votes
0
answers
87
views
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?
...
1
vote
1
answer
2k
views
set jump and long jump codeflow in c
I am trying to learn setjump and longjump in C. Can anyone help me with the output of the following code along with the code flow and the explanation for the the cases.i have called a function funcall(...
0
votes
0
answers
60
views
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: {
...
2
votes
1
answer
366
views
Using setjmp in a wrapper function and local variable clobbering
The setjmp(3) documentation in the manpages (on my system) states that
All accessible objects have values as of the time longjmp() routine was called, except that the values of objects of ...
1
vote
2
answers
144
views
error when creating fibonacci generator sequence using setjmp() and longjmp()
I was trying to make something like fibonacci generator sequence (that is implemented using yield keyword in python) in c language using setjmp() and longjmp() :
#include <setjmp.h>
#include &...
3
votes
1
answer
567
views
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 ...
1
vote
3
answers
295
views
A special switch statement in C
I am reading CSAPP, the chapter about setjmp and longjmp, and encounter the following code snippets:
int rc;
switch(setjmp(buf));
if (rc == 0)
foo();
else if (rc == 1)
printf("sth.");
else if ...