memcpy_s()
is formally defined in the C11 standard (ISO/IEC 9899:2011), section K.3.7.1.1, which simply states:
The memcpy_s function returns zero if there was no runtime-constraint violation. Otherwise, a nonzero value is returned.
There is no mention of the specific values that memcpy_s()
return on error, so they are implementation-defined.
You did not say which compiler you are actually using.
For example, Microsoft’s memcpy_s()
documentation for MSVC states that memcpy_s()
can only return either EINVAL
or ERANGE
when an error occurs, and that behavior is undefined if the source and destination overlap.
MinGW-w64 exhibits this same behavior.
However, this behavior is formally forbidden by the memcpy_s()
definition in ISO/IEC 9899:2011:
Copying shall not take place between objects that overlap.
In some runtime implementations, there is another error code that may be returned:
#define ESOVRLP ( 404 ) /* overlap undefined */
This error means the source and destination are overlapping.
Since you are getting a return value that is undocumented by Microsoft for behavior that is undefined in MSVC (and MinGW), it stands to reason that you are probably using some other compiler/runtime that has a more C11-compliant implementation that is returning ESOVRLP
if overlap is detected (for example, safeclib does).