Skip to main content
GitHub

ERR52-CPP. Do not use setjmp() or longjmp()

The C standard library facilities setjmp() and longjmp() can be used to simulate throwing and catching exceptions. However, these facilities bypass automatic resource management and can result in undefined behavior , commonly including resource leaks and denial-of-service attacks .

The C++ Standard, [support.runtime], paragraph 4 [ ISO/IEC 14882-2014 ], states the following :

The function signature longjmp(jmp_buf jbuf, int val) has more restricted behavior in this International Standard. A setjmp / longjmp call pair has undefined behavior if replacing the setjmp and longjmp by catch and throw would invoke any non-trivial destructors for any automatic objects.

Do not call setjmp() or longjmp() ; their usage can be replaced by more standard idioms such as throw expressions and catch statements.