Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC++2023-8.18.1

In this section:
Synopsis

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

Enabled by default

Yes

Severity/Certainty

High/High

highhigh.png
Full description

There are assignments from one field of a union to another. This check is identical to MISRAC++2008-0-2-1, MISRAC2004-18.2, MISRAC2012-Rule-19.1, UNION-overlap-assign.

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.

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