Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC++2008-7-5-1_a (C++ only)

In this section:
Synopsis

(Required) A function shall not return a reference or a pointer to an automatic variable (including parameters), defined within the function.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

A stack object is returned from a function as a reference. This check is identical to MEM-stack-ref, MISRAC++2023-6.8.2_a.

Coding standards
CERT DCL30-C

Declare objects with appropriate storage durations

CWE 562

Return of Stack Variable Address

MISRA C++ 2023 6.8.2

(Mandatory) A function must not return a reference or a pointer to a local variable with automatic storage duration

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;
}