MISRA.ASSIGN.OVERLAPAn 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 objectC90 [Undefined 34, 55], C99 [Undefined 51, 94] Category: Mandatory Analysis: Undecidable, System Applies to: C90, C99 RationaleThe behaviour is undefined when two objects are created which have some overlap in memory and one is assigned or copied to the other. ExceptionThe following are permitted because the behaviour is well-defined:
ExampleThis 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 alsoRule 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)] RationaleAssigning between objects that have an overlap in their physical storage leads to undefined behaviour. Examplestruct 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 } |