Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.ASSIGN.OVERLAP

An object shall not be assigned to an overlapping object.

MISRA C 2012 Rule 19.1: An object shall not be assigned or copied to an overlapping object

C90 [Undefined 34, 55], C99 [Undefined 51, 94]

Category: Mandatory

Analysis: Undecidable, System

Applies to: C90, C99

Rationale

The behaviour is undefined when two objects are created which have some overlap in memory and one is assigned or copied to the other.

Exception

The following are permitted because the behaviour is well-defined:
  1. Assignment between two objects that overlap exactly and have compatible types (ignoring their type qualifiers)
  2. Copying between objects that overlap partially or completely using The Standard Library function memmove

Example

This example also violates Rule 19.2 because it uses unions.

void fn ( void ) 
{
  union 
  {
    int16_t i;
    int32_t j; 
  } a = { 0 }, b = { 1 };

  a.j = a.i;             /* Non-compliant            */ 
  a = b;                 /* Compliant - exception 1 */ 
}

#include <string.h>

int16_t a[ 20 ];

void f ( void ) 
{
  memcpy ( &a[ 5 ], &a[ 4 ], 2u * sizeof ( a[ 0 ] ) ); /* Non-compliant */ 
}

void g ( void ) 
{
  int16_t *p = &a[ 0 ]; 
  int16_t *q = &a[ 0 ];

  *p = *q;              /* Compliant - exception 1 */ 
}

See also

Rule 19.2

MISRA-C 2004 Rule 18.2 (required): An object shall not be assigned to an overlapping object.

Object is assigned to an overlapping object.

[Undefined 34, 55]

The behaviour is undefined when two objects are created which have some overlap in memory and one is copied to the other.

MISRA-C++ 2008 Rule 0-2-1 (required): An object shall not be assigned to an overlapping object.

This rule is also covered by MISRA.UNION.

[Undefined 5.17(8)]

Rationale

Assigning between objects that have an overlap in their physical storage leads to undefined behaviour.

Example

struct s
{
   int16_t m1 [ 32 ];
};
struct t
{
   int32_t m2;
   struct s m3;
};
void fn ( )
{
   union // Breaks Rule 9—5—1
   {
      struct s u1;
      struct t u2;
   } a;
   a.u2.m3 = a.u1; // Non-compliant
}