+2018-11-28 Paul Eggert <eggert@cs.ucla.edu>
+
+ strerror_r-posix: memmove, not memcpy
+ * lib/strerror_r.c (safe_copy): Use memmove, not memcpy,
+ since the source and destination might overlap in the call
+ ‘safe_copy (buf, buflen, strerror_r (errnum, buf, buflen))’.
+ Simplify.
+
2018-11-25 Akim Demaille <akim@lrde.epita.fr>
bitsetv: new module
safe_copy (char *buf, size_t buflen, const char *msg)
{
size_t len = strlen (msg);
- int ret;
+ size_t moved = len < buflen ? len : buflen - 1;
- if (len < buflen)
- {
- /* Although POSIX allows memcpy() to corrupt errno, we don't
- know of any implementation where this is a real problem. */
- memcpy (buf, msg, len + 1);
- ret = 0;
- }
- else
- {
- memcpy (buf, msg, buflen - 1);
- buf[buflen - 1] = '\0';
- ret = ERANGE;
- }
- return ret;
+ /* Although POSIX lets memmove corrupt errno, we don't
+ know of any implementation where this is a real problem. */
+ memmove (buf, msg, moved);
+ buf[moved] = '\0';
+ return len < buflen ? 0 : ERANGE;
}