Source and destination arguments of a copy function have overlapping memory
This defect occurs
when there is a memory overlap between the source and destination
argument of a copy function such as memcpy or strcpy.
For instance, the source and destination arguments of strcpy are
pointers to different elements in the same string.
If there is memory overlap between the source and destination arguments of copy functions, according to C standards, the behavior is undefined.
Determine if the memory overlap is what you want. If so, find an alternative function. For instance:
If you are using memcpy to copy
values from one memory location to another, use memmove instead
of memcpy.
If you are using strcpy to copy
one string to another, use memmove instead of strcpy,
as follows:
s = strlen(source); memmove(destination, source, s + 1);
strlen determines the string length without
the null terminator. Therefore, you must move s+1 bytes
instead of s bytes.
| Group: Programming |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: OVERLAPPING_COPY |
| Impact: Medium |
| CWE ID: 475, 628, 687 |