Memory overlap between left and right sides of an assignment
This defect occurs when there is a memory overlap between the left and right sides of an assignment. For instance, a variable is assigned to itself or one member of a union is assigned to another.
If the left and right sides of an assignment have memory overlap, the behavior is either redundant or undefined. For instance:
Self-assignment such as x=(int)(long)x; is
redundant unless x is volatile-qualified.
Assignment of one union member to another causes undefined behavior.
For instance, in the following code:
The result of the assignment u1.a = u1.b is
undefined because u1.b is not initialized.
The result of the assignment u2.b = u2.a depends
on the alignment and endianness of the implementation. It is not defined
by C standards.
union {
char a;
int b;
}u1={'a'}, u2={'a'}; //'u1.a' and 'u2.a' are initialized
u1.a = u1.b;
u2.b = u2.a;
Avoid assignment between two variables that have overlapping memory.
| Group: Programming |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: OVERLAPPING_ASSIGN |
| Impact: Low |
| CWE ID: 665 |