Non-local jump via setjmp/longjmp
ID |
c.maintainability.setjmp_longjmp |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
high |
Remediation Effort |
medium |
Resource |
Control Flow |
Language |
C / C++ |
Rationale
A longjmp transfers control straight to the matching setjmp point, unwinding every stack frame in between without running their cleanup. In C++ this skips destructors entirely, which is undefined behavior for any frame holding a non-trivial destructible object, and even in plain C it bypasses atexit-style resource release, RAII-like manual cleanup, and any early return the reader expects the normal call chain to take. The resulting control flow is very hard to trace and to test.
The following code illustrates the pattern detected by this rule:
jmp_buf env;
void do_recover(void)
{
// FLAGGED: Non-local jump via setjmp/longjmp
longjmp(env, 1);
}