bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] strerror_r-posix: memmove, not memcpy


From: Paul Eggert
Subject: [PATCH] strerror_r-posix: memmove, not memcpy
Date: Wed, 28 Nov 2018 16:11:06 -0800

* 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.
---
 ChangeLog        |  8 ++++++++
 lib/strerror_r.c | 21 ++++++---------------
 2 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 681603031..aec0ac21b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2018-11-28  Paul Eggert  <address@hidden>
+
+       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  <address@hidden>
 
        bitsetv: new module
diff --git a/lib/strerror_r.c b/lib/strerror_r.c
index 7a11be1ca..2a22cb414 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;
 }
 
 
-- 
2.19.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]