Skip to main content

IAR Embedded Workbench for Arm 9.70.x

SPC-uninit-struct-field

In this section:
Synopsis

A field of a local struct is read before it is initialized.

Enabled by default

No

Severity/Certainty

High/Medium

highmedium.png
Full description

A field of a local struct is read before it is initialized. An uninitialized field might cause unexpected and unpredictable results. Uninitialized variables are easy to overlook, because they seldom cause problems. This check is identical to MISRAC2004-1.2_b, MISRAC2012-Rule-9.1_d, CERT-EXP33-C_e.

Coding standards
CERT EXP33-C

Do not reference uninitialized memory

CWE 457

Use of Uninitialized Variable

MISRA C:2012 Rule-9.1

(Mandatory) The value of an object with automatic storage duration shall not be read before it has been set

Code examples

The following code example fails the check and will give a warning:

struct st {
  int x;
  int y;
};

void example(void) {
  int a;
  struct st str;
  a = str.x;
}

The following code example passes the check and will not give a warning about this issue:

struct st {
  int x;
  int y;
};

void example(int i) {
  int a;
  struct st str;
  str.x = i;
  a = str.x;
}