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++

Description

Reports a call to setjmp or longjmp, the C standard library’s non-local jump facility.

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);
}

Remediation

Replace the jump with ordinary structured control flow: return an error/status code up the call chain, or in C++ throw an exception and let the stack unwind through destructors.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved