MISRAC++2023-6.8.2_a
In this section:
Synopsis
(Mandatory) A function must not return a reference or a pointer to a local variable with automatic storage duration
Enabled by default
Yes
Severity/Certainty
High/High

Full description
A stack object is returned from a function as a reference. This check is identical to MEM-stack-ref, MISRAC++2008-7-5-1_a.
Coding standards
- CERT DCL30-C
Declare objects with appropriate storage durations
- CWE 562
Return of Stack Variable Address
- MISRA C++ 2008 7-5-1
(Required) A function shall not return a reference or a pointer to an automatic variable (including parameters), defined within the function.
Code examples
The following code example fails the check and will give a warning:
int& example(void) {
int x;
return x;
}
The following code example passes the check and will not give a warning about this issue:
int example(void) {
int x;
return x;
}