Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If a function throws an exception other than one allowed by its exception-specification, it can lead to an implementation-defined termination of the program ([except.spec] paragraph 9).

If a function declared with a dynamic-exception-specification throws an exception of a type that would not match the exception-specification, the function std::unexpected() is called. The behavior of this function can be overridden but, but by default, causes an exception of std::bad_exception to be thrown. Unless std::bad_exception is listed in the exception-specification, the function std::terminate() will be called.

Similarly, if a function declared with a noexcept-specification throws an exception of a type that would lead to cause the noexcept-specification to evaluate to false, the function std::terminate() will be called.

Calling std::terminate() leads to implementation-defined termination of the program. To prevent abnormal termination of the program, any function that declares an exception-specification should restrict itself, as well as any functions it calls, to throwing only allowed exceptions.

...

In this noncompliant code example, the second function claims to throw only exception1, but it may also throw exception2.:

Code Block
bgColor#FFcccc
langcpp
#include <exception>
 
class exception1 : public std::exception {};
class exception2 : public std::exception {};

void foo() {
  throw exception2; // OK,Okay sincebecause foo() promises nothing wrtabout exceptions
}

void bar() throw (exception1) {
  foo();    // Bad, sincebecause foo() can throw exception2
}

...

Code Block
bgColor#ccccff
langcpp
void bar() throw (exception1) {
  try {
    foo();
  } catch (exception2 e) {
    // handleHandle error, without re-throwingrethrowing it.
  }
}

Compliant Solution

This compliant solution declares an exception-specification for bar(), which covers all of the exceptions that can be thrown from it:

...

In this noncompliant code example, a function is declared as non-throwingnonthrowing, but it is possible for std::vector::resize() to throw an exception when the requested memory cannot be allocated:

...

In this compliant solution, the function's noexcept-specification is removed, signifying that the function allows all exceptions:

Code Block
bgColor#ccccff
langcpp
#include <vector>

void f(std::vector<int> &v, size_t s) {
  v.resize(s); // May throw, but that is OK.okay
}

Implementation Details

Some vendors provide language extensions for specifying whether or not a function throws or not. For instance, Microsoft Visual Studio provides __declspec(nothrow)), and Clang supports __attribute__((nothrow)). Currently, the vendors do not document the behavior of specifying a nonthrowing function using these extensions. It is presumed that it is undefined behavior when throwing Throwing from a function declared with one of these language extensions is presumed to be undefined behavior.

Risk Assessment

Throwing unexpected exceptions disrupts control flow and can cause premature termination and denial of service.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ERR55-CPP

Low

Likely

Low

P9

L2

...

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...