Main Content

CERT C++: ERR52-CPP

Do not use setjmp() or longjmp()

Description

Rule Definition

Do not use setjmp() or longjmp().1

Polyspace Implementation

The rule checker checks for Use of setjmp/longjmp.

Examples

expand all

Issue

Use of setjmp/longjmp 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 and longjmp in combination with a signal handler, unexpected control flow can occur. POSIX® does not specify whether setjmp saves the signal mask.

  • Using setjmp and longjmp or sigsetjmp and siglongjmp 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.

Example - Use of setjmp and longjmp
#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.

Correction — Use Alternative to 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

Group: 08. Exceptions and Error Handling (ERR)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.