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
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.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- 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)