Use of setjmp/longjmp
setjmp
and longjmp
cause
deviation from normal control flow
Description
This defect occurs
when you use a combination of setjmp
and longjmp
or sigsetjmp
and siglongjmp
to
deviate from normal control flow and perform non-local jumps in your
code.
Risk
Using setjmp
and longjmp
,
or sigsetjmp
and siglongjmp
has
the following risks:
Nonlocal jumps are vulnerable to attacks that exploit common errors such as buffer overflows. Attackers can redirect the control flow and potentially execute arbitrary code.
Resources such as dynamically allocated memory and open files might not be closed, causing resource leaks.
If you use
setjmp
andlongjmp
in combination with a signal handler, unexpected control flow can occur. POSIX® does not specify whethersetjmp
saves the signal mask.Using
setjmp
andlongjmp
orsigsetjmp
andsiglongjmp
makes your program difficult to understand and maintain.
Fix
Perform nonlocal jumps in your code using setjmp/longjmp
or sigsetjmp/siglongjmp
only
in contexts where such jumps can be performed securely. Alternatively,
use POSIX threads if possible.
In C++, to simulate throwing and catching exceptions, use standard
idioms such as throw
expressions and catch
statements.
Examples
Result Information
Group: Good practice |
Language: C | C++ |
Default: Off |
Command-Line Syntax: SETJMP_LONGJMP_USE |
Impact: Low |
Version History
Introduced in R2015b
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)