AutosarC++17_03-A15.5.2

Program shall not be abruptly terminated. In particular, an implicit or explicit invocation of std::abort(), std::quick_exit(), std::_Exit(), std::terminate() shall not be done

Required inputs: IR, StaticSemanticAnalysis

Rationale

Functions that are used to terminate the program in an immediate fashion, i.e.

std::abort(), std::quick_exit(), std::_Exit(), do so without calling exit handlers or calling destructors of automatic, thread or static storage duration objects. It is implementation- defined whether opened streams are flushed and closed, and temporary files are removed.

The std::terminate() function calls std::abort() implicitly in its terminate handler, and it is implementation-defined whether or not stack unwinding will occur.

Exceptions

Calling an std::exit() function from main() or from task main functions is acceptable, because it properly deallocates resources and calls std::atexit() handlers.

Example
//% $Id: A15-5-2.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#include <cstdlib>
#include <exception>
void f1() noexcept(false);
void f2() // Non-compliant
{
f1(); // A call to throwing f1() may result in an implicit call to
// std::terminate()
}
void f3() // Compliant
{
try {
f1(); // Handles all exceptions from f1() and does not re-throw
} catch (...) {
// Handle an exception
}
}
void f4(const char *log) {
// Report a log error
// ...
std::exit(0); // Call std::exit() function which safely cleans up resources
}
void f5() // Compliant by exception
{
try {
f1();
} catch (...) {
f4("f1() function failed");
}
}
int main(int, char **) {
if (std::atexit(&f2) != 0) {
// Handle an error
}

if (std::atexit(&f3) != 0) {
// Handle an error
}

// ...
return 0;
}
See Also
  • MISRA C++ 2008 [6]: 15-5-3 (Required) The terminate() function shall not be called implicitly.
  • HIC++ v4.0 [8]: 15.3.2 Ensure that a program does not result in a call to std::terminate
  • SEI CERT C++ [9]: ERR50-CPP. Do not abruptly terminate the program

Possible Messages

Key

Text

Severity

Disabled

exception_escaping_constructor

Escaping exception from constructor.

None

False

exception_escaping_destructor

Escaping exception from destructor.

None

False

exception_escaping_initialization

Uncaught exception raised in initialization or finalization

None

False

exception_escaping_main

Uncaught exception escaping from main or additional entry point

None

False

exception_specification_violation

Exception violates function’s exception-specification.

None

False

exception_specification_violation_from_call

Exceptions propagated from this call violate function’s exception-specification.

None

False

forbidden_libfunc_call

Call to forbidden function.

None

False

Options

allowed_exceptions

allowed_exceptions : set[str] = {'bad_alloc', 'bad_cast', 'failure', 'runtime_error', 'system_error'}

Exceptions that are allowed to escape from destructors.
 

blacklist

blacklist

Type: dict[bauhaus.analysis.config.FileGlobPattern, list[bauhaus.analysis.config.GlobPattern]]

Default:

{
   '*abort.h': ['abort'],
   '*stdlib.h': ['abort', 'quick_exit', '_Exit'],
   '*terminate.h': ['terminate'],
   'exception': ['terminate']
}
Dictionary of header globbing to (list of) function name globbing(s) of forbidden functions.
 

constructors

constructors : bool = False

Whether to consider constructors.
 

destructors

destructors : bool = True

Whether to consider destructors.
 

exclude_exception_base_classes

exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()

Exclude issues for the exception types mentioned in this set of qualified names. Also excludes classes derived from those class names as well as pointers or references to any of these class types.
 

generate_violation_path

generate_violation_path : bool = True

Whether to compute a trace for the exception. This improves the usability of the violation description, but requires additional computing which might slow down the rule.
 

ignore_constructor_destructor

ignore_constructor_destructor : bool = False

Whether to ignore escaping exceptions from constructors and destructors.
 

ignore_unknown_routines

ignore_unknown_routines : bool = False

Whether to ignore extern or only declared routines.
 

inspect_at_exit_handlers

inspect_at_exit_handlers : bool = True

Whether to also inspect at_exit() handlers-functions.
 

inspect_atexit_entry_points

inspect_atexit_entry_points : bool = False

Whether to inspect routines set by the atexit/std::atexit function.
 

inspect_thread_main

inspect_thread_main : bool = True

Whether to also inspect thread main functions.
 

report_at_call

report_at_call : bool = False

If set to true, the error is reported at the call-sites of routines throwing exceptions rather than at the throw.
 

report_only_one_exception_per_function

report_only_one_exception_per_function : bool = False

Report at most one uncaught exception per function. This suppresses issues at sites where an uncaught exception is thrown to get a faster execution of the check. Setting this parameter to True will result in false negatives: Real issues may not be detected anymore.