Skip to main content

IAR Embedded Workbench for RISC-V 3.40

UNION-overlap-assign

In this section:
Synopsis

Assignments from one field of a union to another.

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

There are assignments from one field of a union to another. Assignments between objects that are stored in the same physical memory causes undefined behavior. This check is identical to MISRAC2004-18.2, MISRAC++2008-0-2-1, MISRAC2012-Rule-19.1, MISRAC++2023-8.18.1.

Coding standards
MISRA C:2004 18.2

(Required) An object shall not be assigned to an overlapping object.

MISRA C:2012 Rule-19.1

(Mandatory) An object shall not be assigned or copied to an overlapping object

MISRA C++ 2008 0-2-1

(Required) An object shall not be assigned to an overlapping object.

MISRA C++ 2023 8.18.1

(Mandatory) An object or subobject must not be copied to an overlapping object

Code examples

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

void example(void)
{
  union
  {
    char c[5];
    int i;
  } u;
  u.i = u.c[2];
}

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

void example(void)
{
  union
  {
    char c[5];
    int i;
  } u;
  int x;
  x = (int)u.c[2];
  u.i = x;
}