Main Content

CERT C++: ERR50-CPP

Do not abruptly terminate the program

Description

Rule Definition

Do not abruptly terminate the program.1

Polyspace Implementation

The rule checker checks for Implicit call to terminate() function.

Examples

expand all

Issue

The checker flags situations that might result in calling the function std::terminate() implicitly. These situations might include:

  • An exception remains unhandled. For instance:

    • While handling an exception, it escapes through another function that raises an unhandled exception. For instance, a catch statement or exception handler invokes another function that raises an unhandled exception.

    • An empty throw statement raises an unhandled exception again.

  • A class destructor raises an exception.

  • A termination handler that is passed to std::atexit raises an unhandled exception.

Risk

Depending on the hardware and software that you use, calling terminate() implicitly might result in a call to std::abort(), which aborts program execution without deleting the variables in the stack. Such an abnormal termination results in memory leaks and security vulnerabilities.

Fix

To avoid implicit calls to terminate():

  • Avoid unhandled exceptions. For instance, execute the operations of main() or task main functions in a try-catch block. In the catch blocks:

    • Handle exceptions of type std::exception explicitly in appropriate catch blocks.

    • Handle the base class of exceptions arising from third-party libraries.

    • Handle unexpected exceptions in a catch(...) block.

  • Declare destructors as noexcept and handle the exceptions in destructors.

  • Handle all exceptions in termination handlers.

Example — Implicit Call to terminate
#include <stdexcept>
int main(){   // Noncompliant
  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }
  return 0;
}

In this example, main() handles specific types of exceptions. An unexpected exception remains unhandled, resulting in an implicit call to the function terminate that terminates the program abruptly. Because main() calls terminate implicitly, Polyspace® raises this defect.

Correction — Handle Unexpected Exceptions

One possible correction is to include a catch(...) block to handle unexpected exceptions so that the program can exit gracefully.

#include <stdexcept>
[[noreturn]] void gracefulExit(){
	// unwind stack and report errors
	std::terminate();
}
int main()   // Compliant
{
  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }
  catch(...){
	  //Exit gracefully
	  gracefulExit();
  }
  return 0;
}

Example — Unhandled Exceptions in Termination Handlers

The termination handler atexit_handler raises an uncaught exception. The function atexit_handler executes after the main finishes execution. Unhandled exceptions in this function cannot be handled elsewhere, leading to an implicit call to std::terminate(). Polyspace flags the function.

#include <stdexcept>
void atexit_handler(){//Noncompliant
	throw std::runtime_error("Error in atexit function");
}
void main(){
	try{
		//...
		std::atexit(atexit_handler);
	}catch(...){
		
	}
}
Correction — Handle All Exceptions in Termination Handlers

To correct the issue, use a catch(...) block to handle all exceptions in the termination handler atexit_handler.

#include <stdexcept>
void atexit_handler(){
	try{
		//..
		throw std::runtime_error("Error in atexit function");
	}catch(...){
		//...
	}
}
void main(){
	try{
		//...
		std::atexit(atexit_handler);
	}catch(...){
		
	}
}

Check Information

Group: 08. Exceptions and Error Handling (ERR)

Version History

Introduced in R2019a

expand all


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.