Versions Compared

Key

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

Not all exceptions can be caught, even with careful use of function-try-blocks. The C++ Standard, [except.handle], paragraph 13 [ISO/IEC 14882-2014], states the following:

Exceptions thrown in destructors of objects with static storage duration or in constructors of namespace scope objects with static storage duration are not caught by a function-try-block on main() . Exceptions thrown in destructors of objects with thread storage duration or in constructors of namespace-scope objects with thread storage duration are not caught by a function-try-block on the initial function of the thread.

When declaring an object with static or thread storage duration, and that object is not declared within a function block scope, the type's constructor must be declared noexcept and must comply with ERR55-CPP. Honor exception specifications. Additionally, the initializer for such a declaration, if any, must not throw an uncaught exception (including from any implicitly - constructed objects that are created as a part of the initialization). If an uncaught exception is thrown before main() is executed, or if an uncaught exception is thrown after main() has finished executing, there are no further opportunities to handle the exception and it results in implementation-defined behavior; see . (See ERR50-CPP. Do not abruptly terminate the program for further details.)

For more information on exception specifications of destructors, see DCL57-CPP. Do not let exceptions escape from destructors or deallocation functions.

...

In this noncompliant example, the constructor of global may throw an exception during program startup. (the The std::string constructor accepting , which accepts a const char * and a default allocator object, is not marked noexcept and consequently allows all exceptions.) . This exception is not caught by the function-try-block on main(), resulting in a call to std::terminate() and abnormal program termination.

...

Compliant code must prevent exceptions from escaping during program startup and termination. This compliant solution avoids defining a std::string at global namespace scope and instead uses a static const char *:.

Code Block
bgColor#ccccff
langcpp
static const char *global = "...";

int main() {
  // ...
}

...

Throwing an exception that cannot be caught results in abnormal program termination and can lead to denial-of-service attacks.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ERR58-CPP

Low

Likely

Low

P9

L2

Automated Detection

Tool

Version

Checker

Description

Astrée

Include Page
Astrée_V
Astrée_V

potentially-throwing-static-initialization
Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC++-ERR58
Clang
Include Page
Clang_38_V
Clang_38_V
cert-err58-cppChecked by clang-tidy
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.EXCP.THROW

Use of throw

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C++4634, C++4636, C++4637, C++4639


Parasoft C/C++test
9.5MISRA2008-15_3_1, EXCEPT-18 
Include Page
Parasoft_V
Parasoft_V
CERT_CPP-ERR58-a

Exceptions shall be raised only after start-up and before termination of the program

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C++: ERR58-CPPChecks for exceptions raised during program startup (rule fully covered)
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
potentially-throwing-static-initialization
Partially checked

Related Vulnerabilities

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

...

This rule is a subset of ERR50-CPP. Do not abruptly terminate the program

Bibliography

[ISO/IEC 14882-2014]Subclause 15.4, "Exception Specifications"
[Sutter
00
2000]Item 8, "Writing Exception-Safe Code—Part 1"

...


...

 Image Modified Image Modified