MISRA C++:2023 Rule 9.6.4
Description
Rule Definition
A function declared with the [[noreturn]]
attribute shall not
return.
Rationale
If you declare a function by using the [[noreturn]]
attribute, the compiler expects that the function does not return the flow of execution. That is, if a [[noreturn]]
function f()
is called from main()
, then the compiler expects that the flow of execution is not returned to main()
. If such a function eventually returns the flow of execution, it leads to undefined behavior, which can be
exploited to cause data integrity violations.
If a function has no return
statement, then the final closing brace of a function implies an implicit return
. Omitting a return
statement in the function does not prevent the flow of execution from returning. A [[noreturn]]
function can prohibit returning the flow of execution to the calling function by:
Entering an infinite loop
Raising an exception
Calling another
[[noreturn]]
function
Polyspace Implementation
If a function specified as [[noreturn]]
returns the control flow to its caller, Polyspace® flags the [[noreturn]]
function.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2024b