1setjmp(3) Library Functions Manual setjmp(3)
2
3
4
6 setjmp, sigsetjmp, longjmp, siglongjmp - performing a nonlocal goto
7
9 Standard C library (libc, -lc)
10
12 #include <setjmp.h>
13
14 int setjmp(jmp_buf env);
15 int sigsetjmp(sigjmp_buf env, int savesigs);
16
17 [[noreturn]] void longjmp(jmp_buf env, int val);
18 [[noreturn]] void siglongjmp(sigjmp_buf env, int val);
19
20 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22 setjmp(): see NOTES.
23
24 sigsetjmp():
25 _POSIX_C_SOURCE
26
28 The functions described on this page are used for performing "nonlocal
29 gotos": transferring execution from one function to a predetermined lo‐
30 cation in another function. The setjmp() function dynamically estab‐
31 lishes the target to which control will later be transferred, and
32 longjmp() performs the transfer of execution.
33
34 The setjmp() function saves various information about the calling envi‐
35 ronment (typically, the stack pointer, the instruction pointer, possi‐
36 bly the values of other registers and the signal mask) in the buffer
37 env for later use by longjmp(). In this case, setjmp() returns 0.
38
39 The longjmp() function uses the information saved in env to transfer
40 control back to the point where setjmp() was called and to restore
41 ("rewind") the stack to its state at the time of the setjmp() call. In
42 addition, and depending on the implementation (see NOTES), the values
43 of some other registers and the process signal mask may be restored to
44 their state at the time of the setjmp() call.
45
46 Following a successful longjmp(), execution continues as if setjmp()
47 had returned for a second time. This "fake" return can be distin‐
48 guished from a true setjmp() call because the "fake" return returns the
49 value provided in val. If the programmer mistakenly passes the value 0
50 in val, the "fake" return will instead return 1.
51
52 sigsetjmp() and siglongjmp()
53 sigsetjmp() and siglongjmp() also perform nonlocal gotos, but provide
54 predictable handling of the process signal mask.
55
56 If, and only if, the savesigs argument provided to sigsetjmp() is non‐
57 zero, the process's current signal mask is saved in env and will be re‐
58 stored if a siglongjmp() is later performed with this env.
59
61 setjmp() and sigsetjmp() return 0 when called directly; on the "fake"
62 return that occurs after longjmp() or siglongjmp(), the nonzero value
63 specified in val is returned.
64
65 The longjmp() or siglongjmp() functions do not return.
66
68 For an explanation of the terms used in this section, see at‐
69 tributes(7).
70
71 ┌────────────────────────────────────────────┬───────────────┬─────────┐
72 │Interface │ Attribute │ Value │
73 ├────────────────────────────────────────────┼───────────────┼─────────┤
74 │setjmp(), sigsetjmp() │ Thread safety │ MT-Safe │
75 ├────────────────────────────────────────────┼───────────────┼─────────┤
76 │longjmp(), siglongjmp() │ Thread safety │ MT-Safe │
77 └────────────────────────────────────────────┴───────────────┴─────────┘
78
80 setjmp()
81 longjmp()
82 C11, POSIX.1-2008.
83
84 sigsetjmp()
85 siglongjmp()
86 POSIX.1-2008.
87
89 setjmp()
90 longjmp()
91 POSIX.1-2001, C89.
92
93 sigsetjmp()
94 siglongjmp()
95 POSIX.1-2001.
96
97 POSIX does not specify whether setjmp() will save the signal mask (to
98 be later restored during longjmp()). In System V it will not. In
99 4.3BSD it will, and there is a function _setjmp() that will not. The
100 behavior under Linux depends on the glibc version and the setting of
101 feature test macros. Before glibc 2.19, setjmp() follows the System V
102 behavior by default, but the BSD behavior is provided if the
103 _BSD_SOURCE feature test macro is explicitly defined and none of
104 _POSIX_SOURCE, _POSIX_C_SOURCE, _XOPEN_SOURCE, _GNU_SOURCE, or
105 _SVID_SOURCE is defined. Since glibc 2.19, <setjmp.h> exposes only the
106 System V version of setjmp(). Programs that need the BSD semantics
107 should replace calls to setjmp() with calls to sigsetjmp() with a
108 nonzero savesigs argument.
109
111 setjmp() and longjmp() can be useful for dealing with errors inside
112 deeply nested function calls or to allow a signal handler to pass
113 control to a specific point in the program, rather than returning to
114 the point where the handler interrupted the main program. In the
115 latter case, if you want to portably save and restore signal masks, use
116 sigsetjmp() and siglongjmp(). See also the discussion of program
117 readability below.
118
120 The compiler may optimize variables into registers, and longjmp() may
121 restore the values of other registers in addition to the stack pointer
122 and program counter. Consequently, the values of automatic variables
123 are unspecified after a call to longjmp() if they meet all the
124 following criteria:
125
126 • they are local to the function that made the corresponding setjmp()
127 call;
128
129 • their values are changed between the calls to setjmp() and
130 longjmp(); and
131
132 • they are not declared as volatile.
133
134 Analogous remarks apply for siglongjmp().
135
136 Nonlocal gotos and program readability
137 While it can be abused, the traditional C "goto" statement at least has
138 the benefit that lexical cues (the goto statement and the target label)
139 allow the programmer to easily perceive the flow of control. Nonlocal
140 gotos provide no such cues: multiple setjmp() calls might employ the
141 same jmp_buf variable so that the content of the variable may change
142 over the lifetime of the application. Consequently, the programmer may
143 be forced to perform detailed reading of the code to determine the
144 dynamic target of a particular longjmp() call. (To make the
145 programmer's life easier, each setjmp() call should employ a unique
146 jmp_buf variable.)
147
148 Adding further difficulty, the setjmp() and longjmp() calls may not
149 even be in the same source code module.
150
151 In summary, nonlocal gotos can make programs harder to understand and
152 maintain, and an alternative should be used if possible.
153
154 Undefined Behavior
155 If the function which called setjmp() returns before longjmp() is
156 called, the behavior is undefined. Some kind of subtle or unsubtle
157 chaos is sure to result.
158
159 If, in a multithreaded program, a longjmp() call employs an env buffer
160 that was initialized by a call to setjmp() in a different thread, the
161 behavior is undefined.
162
163 POSIX.1-2008 Technical Corrigendum 2 adds longjmp() and siglongjmp() to
164 the list of async-signal-safe functions. However, the standard
165 recommends avoiding the use of these functions from signal handlers and
166 goes on to point out that if these functions are called from a signal
167 handler that interrupted a call to a non-async-signal-safe function (or
168 some equivalent, such as the steps equivalent to exit(3) that occur
169 upon a return from the initial call to main()), the behavior is
170 undefined if the program subsequently makes a call to a non-async-
171 signal-safe function. The only way of avoiding undefined behavior is
172 to ensure one of the following:
173
174 • After long jumping from the signal handler, the program does not
175 call any non-async-signal-safe functions and does not return from
176 the initial call to main().
177
178 • Any signal whose handler performs a long jump must be blocked during
179 every call to a non-async-signal-safe function and no non-async-
180 signal-safe functions are called after returning from the initial
181 call to main().
182
184 signal(7), signal-safety(7)
185
186
187
188Linux man-pages 6.05 2023-07-20 setjmp(3)