SetJmp/LongJmp: Why is this throwing a segfault?

前端 未结 2 1374
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 02:17

The following code summarizes the problem I have at the moment. My current execution flow is as follows and a I\'m running in GCC 4.3.

jmp_buf a_buf;
jmp_buf         


        
相关标签:
2条回答
  • 2020-12-03 02:36

    You can only longjmp() back up the call stack. The call to longjmp(b_buf, 1) is where things start to go wrong, because the stack frame referenced by b_buf no longer exists after the longjmp(a_buf).

    From the documentation for longjmp:

    The longjmp() routines may not be called after the routine which called the setjmp() routines returns.

    This includes "returning" through a longjmp() out of the function.

    0 讨论(0)
  • 2020-12-03 02:54

    The standard says this about longjmp() (7.13.2.1 The longjmp function):

    The longjmp function restores the environment saved by the most recent invocation of the setjmp macro in the same invocation of the program with the corresponding jmp_buf argument. If there has been no such invocation, or if the function containing the invocation of the setjmp macro has terminated execution in the interim

    with a footnote that clarifies this a bit:

    For example, by executing a return statement or because another longjmp call has caused a transfer to a setjmp invocation in a function earlier in the set of nested calls.

    So you can't longjmp() back & forth across nested setjmp/longjmp sets.

    0 讨论(0)
提交回复
热议问题