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
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
setjmpandlongjmpin combination with a signal handler, unexpected control flow can occur. POSIX® does not specify whethersetjmpsaves the signal mask.Using
setjmpandlongjmporsigsetjmpandsiglongjmpmakes 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 longjmpTo 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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)