Copy of overlapping memory
Source and destination arguments of a copy function have overlapping memory
Description
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.
Risk
If there is memory overlap between the source and destination arguments of copy functions, according to C standards, the behavior is undefined.
Fix
Determine if the memory overlap is what you want. If so, find an alternative function. For instance:
If you are using
memcpyto copy values from one memory location to another, usememmoveinstead ofmemcpy.If you are using
strcpyto copy one string to another, usememmoveinstead ofstrcpy, as follows:s = strlen(source); memmove(destination, source, s + 1);
strlendetermines the string length without the null terminator. Therefore, you must moves+1bytes instead ofsbytes.
Examples
Result Information
| Group: Programming |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: OVERLAPPING_COPY |
| Impact: Medium |
Version History
Introduced in R2015b
See Also
Find defects (-checkers) | Overlapping assignment
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)