CWE Rule 691
Description
Rule Description
The code does not sufficiently manage its control flow during execution, creating conditions in which the control flow can be modified in unexpected ways.
Polyspace Implementation
The rule checker checks for Use of setjmp/longjmp.
Examples
Use of setjmp/longjmp
This issue 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.
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.
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.
#include <setjmp.h> #include <signal.h> extern int update(int); extern void print_int(int); static jmp_buf env; void sighandler(int signum) { longjmp(env, signum); //Noncompliant } void func_main(int i) { signal(SIGINT, sighandler); if (setjmp(env)==0) { //Noncompliant while(1) { /* Main loop of program, iterates until SIGINT signal catch */ i = update(i); } } else { /* Managing longjmp return */ i = -update(i); } print_int(i); return; }
In this example, the initial return value of setjmp
is
0. The update
function is called in an infinite while
loop
until the user interrupts it through a signal.
In the signal handling function, the longjmp
statement
causes a jump back to main
and the return value
of setjmp
is now 1. Therefore, the else
branch
is executed.
setjmp
and longjmp
To emulate the same behavior more securely,
use a volatile
global variable instead of a combination
of setjmp
and longjmp
.
#include <setjmp.h> #include <signal.h> extern int update(int); extern void print_int(int); volatile sig_atomic_t eflag = 0; void sighandler(int signum) { eflag = signum; /* Fix: using global variable */ } void func_main(int i) { /* Fix: Better design to avoid use of setjmp/longjmp */ signal(SIGINT, sighandler); while(!eflag) { /* Fix: using global variable */ /* Main loop of program, iterates until eflag is changed */ i = update(i); } print_int(i); return; }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)