From: Paul Eggert Date: Thu, 29 Nov 2018 00:10:03 +0000 (-0800) Subject: strerror_r-posix: memmove, not memcpy X-Git-Tag: v1.0~5286 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d09f113ef952f460db88f178c7d121f1eafb714d;p=thirdparty%2Fgnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 6816030312..aec0ac21b1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2018-11-28 Paul Eggert + + 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 bitsetv: new module diff --git a/lib/strerror_r.c b/lib/strerror_r.c index 7a11be1caf..2a22cb4146 100644 --- a/lib/strerror_r.c +++ b/lib/strerror_r.c @@ -129,22 +129,13 @@ static int 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; }