Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC2004-9.1_b

In this section:
Synopsis

(Required) All automatic variables shall have been assigned a value before being used.

Enabled by default

Yes

Severity/Certainty

High/Low

highlow.png
Full description

On some execution paths, a variable is read before it is assigned a value. This check is identical to SPC-uninit-var-some, MISRAC++2008-8-5-1_b, MISRAC2012-Rule-9.1_f, MISRAC++2023-11.6.2.

Coding standards
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

MISRA C++ 2008 8-5-1

(Required) All variables shall have a defined value before they are used.

MISRA C++ 2023 11.6.2

(Mandatory) The value of an object must not be read before it has been set

Code examples

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

#include <stdlib.h>

int main(void) {
  int x, y;
  if (rand()) {
    x = 0;
  }
  y = x;  //x may not be initialized
  return 0;
}

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

#include <stdlib.h>

int main(void) {
  int x;
  if (rand()) {
    x = 0;
  }
  /* x never read */
  return 0;
}