This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 9.6.4
Category: Required
Analysis Type: Undecidable,System
Amplification
Leaving a function as the result of an exception is not a return.
A function’s compliance with this rule is determined independently of the context in which the function is called. For example, a boolean parameter
is treated as if it may have a value of true
or false
, even if all the calls expressed in the current program use a value of
true
.
Rationale
Returning from a function declared as [[noreturn]]
results in undefined behaviour.
Note: a function may be declared as [[noreturn]]
when:
- It only exits by throwing an exception; or
- It loops endlessly; or
- It causes program termination.
Example
[[noreturn]] void kill_the_process() // Compliant
{
std::abort(); // Note - std::abort is also [[noreturn]]
}
[[noreturn]] void throw_some() // Compliant - only exits with an exception
{
throw 42;
}
[[noreturn]] void g( bool b ) // Non-compliant - returns if 'b' is false
{
if ( b )
{
throw std::exception{};
}
}
Copyright The MISRA Consortium Limited © 2023