AutosarC++18_10-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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially automated |
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.
Note: std::terminate_handler shall not be used.
Example
//% $Id: A15-5-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#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 [7]: 15-5-3 (Required) The terminate() function shall not be called implicitly.
- HIC++ v4.0 [9]: 15.3.2 Ensure that a program does not result in a call to std::terminate
- SEI CERT C++ [10]: 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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allowed_exceptions¶
allowed_exceptions : set[str] = {'bad_alloc', 'bad_cast', 'failure', 'runtime_error', 'system_error'}
blacklist¶
blacklist
Dictionary of header globbing to (list of) function name globbing(s) of forbidden functions.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'] }
constructors¶
constructors : bool = False
destructors¶
destructors : bool = True
exclude_exception_base_classes¶
exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()
generate_violation_path¶
generate_violation_path : bool = True
ignore_constructor_destructor¶
ignore_constructor_destructor : bool = False
ignore_unknown_routines¶
ignore_unknown_routines : bool = False
inspect_at_exit_handlers¶
inspect_at_exit_handlers : bool = True
at_exit() handlers-functions.
inspect_atexit_entry_points¶
inspect_atexit_entry_points : bool = False
inspect_thread_main¶
inspect_thread_main : bool = True
report_at_call¶
report_at_call : bool = False
report_only_one_exception_per_function¶
report_only_one_exception_per_function : bool = False