Main Content

CERT C++: DCL57-CPP

Do not let exceptions escape from destructors or deallocation functions

Description

Rule Definition

Do not let exceptions escape from destructors or deallocation functions.1

Polyspace Implementation

The rule checker checks for Class destructor exiting with an exception.

Examples

expand all

Issue

The checker flags:

  • Explicit throw statements in the body of a destructor outside of a try-catch block. If the destructor calls another function, the checker does not detect if the called function raises an exception.

  • The exception specification noexcept(false) in the declaration of the destructor.

The checker does not detect:

  • A catch statement that does not catch exceptions of all types that are thrown.

    The checker considers the presence of a catch statement corresponding to a try block as an indication that an exception is caught.

  • throw statements inside catch blocks.

Risk

Destructors are invoked at the end of code execution. When exceptions arise at this stage, they become unhandled. When such unhandled exceptions arise, depending on the hardware and software that you use, the compiler might abruptly terminate the program execution without deleting the objects in stack. Such abrupt termination might result in a resource leak and security vulnerabilities.

Fix

To avoid this issue:

  • Declare destructors as noexcept(true).

  • Handle exceptions that might arise in destructors by using a try-catch block that includes a catch(...) block.

Example
#include<stdexcept>
class C {
	//...
	~C() noexcept(false) { //Noncompliant
		//...
		throw std::logic_error("Error"); //Noncompliant
	}
	
};

In this example, the destructor of class C is specified as noexcept(false). Polyspace® flags the declaration. The destructor contains an explicit throw statement without encasing it in a try-catch block. Polyspace flags the throw statement.

Correction

One possible correction is to declare destructors as noexcept(true), and then encase any throw statement in a try-catch block.

#include<stdexcept>
class C {
	//...
	~C() noexcept(true) { //Compliant
		//...
		try{
			throw std::logic_error("Error"); //Compliant
		}catch(...){
			
		}
	}
	
};

Check Information

Group: 01. Declarations and Initialization (DCL)

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.