This rule is part of MISRA C++:2023.
Usage of this content is governed by Sonar’s terms and conditions. Redistribution is
prohibited.
Rule 6.8.3 - An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater
lifetime
Category: Required
Analysis: Decidable,Single Translation Unit
Amplification
This rule applies when the right-hand side of an assignment operator has the form &x, addressof( x ), or is the name
of an object having array type.
For the purposes of this rule, two objects with automatic storage duration that are declared in the same scope are considered to have the same
lifetime.
Rationale
If the address of an automatic object is assigned to another automatic object of larger scope, or to an object with static storage duration, then
the object containing the address may exist beyond the time when the original object ceases to exist (and its address becomes invalid).
Note: this rule and M23_360: MISRA C++ 2023 Rule 6.8.1.
Example
void f1()
{
int8_t * p;
{
int8_t local;
int8_t local_array[ 10 ];
p = &local; // Non-compliant
p = local_array; // Non-compliant
}
}
The following example is compliant with this rule, but violates M23_360: MISRA C++ 2023 Rule 6.8.1.
void f2()
{
int8_t * p1;
{
int8_t * p2 = nullptr;
int8_t local;
p2 = &local; // Compliant - objects have the same lifetime
p1 = p2; // Rule does not apply
}
*p1 = 0; // Undefined behaviour
}
Copyright The MISRA Consortium Limited © 2023