LONGJMP(3POSIX)
π« PROLOG π«
okay bestie, before we even get into it β this page is part of the POSIX Programmer's Manual. Heads up tho: the Linux implementation of this interface might be different from what's written here (so like, go check the corresponding Linux manual page for the actual Linux tea β), or honestly the interface might just not be implemented on Linux at all. no cap. π
β¨ NAME β¨
longjmp β non-local goto π¦ (yes it's literally a jump-scare but for control flow)
πΈ SYNOPSIS πΈ
#include <setjmp.h>
void longjmp(jmp_buf env, int val);
π DESCRIPTION π
So the functionality on this reference page is aligned with the ISO C standard. If anything here conflicts with the ISO C standard, that was not on purpose β unintentional, fr. This volume of POSIX.1β2017 defers to the ISO C standard, periodt.
The longjmp() function shall restore the environment saved by the most recent invocation of setjmp() in the same process, using the corresponding jmp_buf argument. π«Ά
now here's where it gets spicy πΆοΈ β the behavior is undefined if any of these happen:
- the most recent invocation of
setjmp()with the correspondingjmp_bufoccurred in another thread, or - there's no such invocation at all, or
- the function containing the invocation of
setjmp()has terminated execution in the interim, or - the invocation of
setjmp()was within the scope of an identifier with variably modified type and execution has left that scope in the interim.
It is unspecified whether longjmp() restores the signal mask, leaves the signal mask unchanged, or restores it to its value at the time setjmp() was called. so like, nobody knows, and that's on purpose apparently π
All accessible objects have values, and all other components of the abstract machine have state (for example, floating-point status flags and open files), as of the time longjmp() was called β except that the values of objects of automatic storage duration are unspecified if they meet all of the following conditions:
- They are local to the function containing the corresponding
setjmp()invocation. - They do not have volatile-qualified type.
- They are changed between the
setjmp()invocation andlongjmp()call.
π lowkey this is the trap. those three conditions all stacking together = unspecified values. volatile-qualified or untouched = you're fine. pick your fighter.
Even though longjmp() is an async-signal-safe function, if it's invoked from a signal handler which interrupted a non-async-signal-safe function or equivalent (such as the processing equivalent to exit() performed after a return from the initial call to main()), the behavior of any subsequent call to a non-async-signal-safe function or equivalent is undefined. π
And the effect of a call to longjmp() where initialization of the jmp_buf structure was not performed in the calling thread is undefined. that's the vibe check and it failed, bestie.
π RETURN VALUE π
After longjmp() is completed, program execution continues as if the corresponding invocation of setjmp() had just returned the value specified by val. π
The longjmp() function shall not cause setjmp() to return 0; if val is 0, setjmp() shall return 1. (so like, zero is simply not an option here, iconic behavior honestly β¨)
π ERRORS π
No errors are defined. π«Ά
The following sections are informative. (iykyk β that means they're the bonus lore, not the rules)
π EXAMPLES π
None. π (we got nothing, sorry bestie)
π§ APPLICATION USAGE π§
Applications whose behavior depends on the value of the signal mask should not use longjmp() and setjmp(), since their effect on the signal mask is unspecified. Instead, use the siglongjmp() and sigsetjmp() functions β those ones can save and restore the signal mask under application control. π
It is recommended that applications do not call longjmp() or siglongjmp() from signal handlers. To avoid undefined behavior when calling these functions from a signal handler, the application needs to ensure one of the following two things:
- After the call to
longjmp()orsiglongjmp()the process only calls async-signal-safe functions and does not return from the initial call tomain(). - Any signal whose handler calls
longjmp()orsiglongjmp()is blocked during every call to a non-async-signal-safe function, and no such calls are made after returning from the initial call tomain().
β pick one, do it properly, and you're slaying. skip both and it's undefined behavior city. π
π RATIONALE π
None.
π¦ FUTURE DIRECTIONS π¦
None.
π₯ SEE ALSO π₯
setjmp()sigaction()siglongjmp()sigsetjmp()
The Base Definitions volume of POSIX.1β2017, <setjmp.h> π
β COPYRIGHT β
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
k bye bestie, that's the whole page π this one came from IEEE/The Open Group, 2017, and it's longjmp(3POSIX) from the POSIX Programmer's Manual. go forth and jump non-locally, responsibly π«Άβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms