memcpy called with the same pointer as source and destination
ID |
c.correctness.overlapping_memcpy |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Undefined Behavior |
Language |
C / C++ |
Description
This memcpy call passes the same pointer as both destination and source. The C standard requires the two regions passed to memcpy to not overlap; passing identical pointers is undefined behavior even though the regions trivially overlap completely. Use memmove, which is defined for overlapping regions, or drop the call entirely since a self-copy has no effect.
Rationale
This memcpy call passes the same pointer as both destination and source. The C standard requires the two regions passed to memcpy to not overlap; passing identical pointers is undefined behavior even though the regions trivially overlap completely. Use memmove, which is defined for overlapping regions, or drop the call entirely since a self-copy has no effect.
The following code illustrates the pattern detected by this rule:
void same_pointer(char *buf, size_t n)
{
// FLAGGED: memcpy called with the same pointer as source and destination
memcpy(buf, buf, n);
}