I am just beginning to understand strlcpy.
size_t strlcpy(char *destination, const char *source, size_t size);
My hypothetical question is: What if the destination and source point to the same object?
Example:
char destination[100];
const char*source = "text";
destination = source;
strlcpy(destination, source, sizeof(destination))
What goes on in the backend?
Is strlcpy
aware that the source and destination share the same pointer?
Or
Does it blindly copy over and wastes cpu cycles - copying over the bytes which are the same?
What if the destination and source point to the same object?
strlcpy()
is not part of the C standard library. Its precise functionality may vary from compiler to compiler. Review the documentation of the particular compiler/library to get the best answer.
As part of BSD systems, strlcpy(3) - Linux man page, I did not find anything dis-allowing overlap.
Since C99, keyword restrict
helps answer the "What if the destination and source point to the same object?" part.
If the signature was as below, than using destination, source
that reference overlapped data is undefined behavior. Anything may happen.
size_t strlcpy(char * restrict destination, const char * restrict source, size_t size);
If the signature was as below and compiler is compliant to C99 or later, than using destination, source
that may overlap is defined behavior.
If the signature was as below and compiler is not complaint to C99 or later, than using destination, source
that may overlap is likely undefined behavior unless the documentation addresses this case.
size_t strlcpy(char * destination, const char *source, size_t size);