AutosarC++17_03-A15.5.3¶
The std::terminate() function shall not be called implicitly
Required inputs: IR, StaticSemanticAnalysis
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
It is implementation-defined whether the call stack is unwound before std::terminate() iscalled. There is no guarantee that the destructors of automatic thread or static storageduration objects will be called.
These are following ways to call std::terminate() function implicitly, according to(std::terminate() in CppReference [15]):
- an exception is thrown and not caught (it is implementation-defined whether anystack unwinding is done in this case)
- an exception is thrown during exception handling (e.g. from a destructor of somelocal object, or from a function that had to be called during exception handling)
- the constructor or the destructor of a static or thread-local object throws anexception
- a function registered with std::atexit or std::at_quick_exit throws an exception
- a noexcept specification is violated (it is implementation-defined whether anystack unwinding is done in this case)
- a dynamic exception specification is violated and the default handler forstd::unexpected is executed
- a non-default handler for std::unexpected throws an exception that violates thepreviously violated dynamic exception specification, if the specification does notinclude std::bad_exception
- std::nested_exception::rethrow_nested is called for an object that isn’t holding acaptured exception
- an exception is thrown from the initial function of std::thread
- a joinable std::thread is destroyed or assigned to
Note: std::terminate_handler shall not be used.
Example
//% $Id: A15-5-3.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <stdexcept>
#include <thread>
extern bool f1();
class A {
public:
A() noexcept(false) {
// ...
throw std::runtime_error("Error1");
}
~A() {
// ...
throw std::runtime_error("Error2"); // Non-compliant - std::terminate()
// called on throwing an exception
// from noexcept(true) destructor
}
};
class B {
public:
~B() noexcept(false) {
// ...
throw std::runtime_error("Error3");
}
};
void f2() { throw; }
void threadFunc() {
A a; // Throws an exception from a’s constructor and does not handle it in
// thread_func()
}
void f3() {
try {
std::thread t(&threadFunc); // Non-compliant - std::terminate() called
// on throwing an exception from
// thread_func()
if (f1()) {
throw std::logic_error("Error4");
}
else {
f2(); // Non-compliant - std::terminate() called if there is no
// active exception to be re-thrown by f2
}
} catch (...) {
B b; // Non-compliant - std::terminate() called on throwing an
// exception from b’s destructor during exception handling
// ...
f2();
}
}
static A a; // Non-compliant - std::terminate() called on throwing an exception
// during program’s start-up phase
int main(int, char **) {
f3(); // Non-compliant - std::terminate() called if std::logic_error is
// thrown
return 0;
}
See Also
- MISRA C++ 2008 [6]: 15-5-3 (Required) The terminate() function shall not be called implicitly.
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 |
implicit_noexcept_spec_violation_without |
Function implicitly declared noexcept(false) but no exceptions will be thrown. |
None |
False |
noexcept_spec_violation_with |
Exception violates function’s noexcept-specification. |
None |
False |
noexcept_spec_violation_without |
Function declared noexcept(false) but no exceptions will be thrown. |
None |
False |
possibly_required_operation |
This thread is possibly joinable on destructor call |
None |
False |
required_operation |
This thread is joinable on destructor call |
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'}
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 = True
ignore_implicit_noexcept_spec_lambda¶
ignore_implicit_noexcept_spec_lambda : bool = False
ignore_throwing_functions¶
ignore_throwing_functions : 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
maximum_number_of_threads¶
maximum_number_of_threads : int = 0
report_at_call¶
report_at_call : bool = False
report_noexcept_false_violations¶
report_noexcept_false_violations : bool = False
report_only_one_exception_per_function¶
report_only_one_exception_per_function : bool = False
required¶
required
Dict which lists required operations per resource. The mapping gives each case a description which maps to a dict for key "Required_Functions".Type: dict[str, dict[str, dict[str, bool | str]]]
Default:
{ 'Thread': { 'Writing to a read-only file stream': { 'Required_Functions': 'std::thread::join,std::thread::detach' } } }
resources¶
resources : set[str] = {'Thread'}