The [[noreturn]]
attribute specifies that a function does not return. The C++ Standard, [dcl.attr.noreturn] paragraph 2 [ISO/IEC 14882-2014], states the following:
If a function
f
is called wheref
was previously declared with thenoreturn
attribute andf
eventually returns, the behavior is undefined.
...
In this noncompliant code example, if the value 0
is passed, control will flow off the end of the function, resulting in an implicit return and undefined behavior:.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <cstdlib> [[noreturn]] void f(int i) { if (i > 0) throw "Received positive input"; else if (i < 0) std::exit(0); } |
...
In this compliant solution, the function does not return on any code path:.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <cstdlib> [[noreturn]] void f(int i) { if (i > 0) throw "Received positive input"; std::exit(0); } |
Risk Assessment
Returning from a function marked [[noreturn]]
results in undefined behavior that might be exploited to cause data-integrity violations.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MSC53-CPP | Medium | Unlikely | Low | P2 | L3 |
...