summaryrefslogtreecommitdiff
path: root/lib/strerror_r.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2018-11-28 16:10:03 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2018-11-28 16:10:51 -0800
commitd09f113ef952f460db88f178c7d121f1eafb714d (patch)
treec30b2a4e996eabb3563ad1b1f17e2b9c406cd21a /lib/strerror_r.c
parent21fa3d5ca9c1bb4a3d6192b87adec42126c4f99d (diff)
downloadgnulib-d09f113ef952f460db88f178c7d121f1eafb714d.tar.gz
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.
Diffstat (limited to 'lib/strerror_r.c')
-rw-r--r--lib/strerror_r.c21
1 files changed, 6 insertions, 15 deletions
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;
}