about summary refs log tree commit diff
path: root/sysdeps/generic/_strerror.c
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/generic/_strerror.c')
-rw-r--r--sysdeps/generic/_strerror.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/sysdeps/generic/_strerror.c b/sysdeps/generic/_strerror.c
index bcba45d519..e6f56556aa 100644
--- a/sysdeps/generic/_strerror.c
+++ b/sysdeps/generic/_strerror.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
+/* Copyright (C) 1991, 93, 95, 96, 97, 98 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -19,7 +19,8 @@
 #include <libintl.h>
 #include <stdio.h>
 #include <string.h>
-#include "../stdio-common/_itoa.h"
+#include <sys/param.h>
+#include <stdio-common/_itoa.h>
 
 #ifndef HAVE_GNU_LD
 #define _sys_errlist sys_errlist
@@ -41,12 +42,27 @@ _strerror_internal (int errnum, char *buf, size_t buflen)
 {
   if (errnum < 0 || errnum >= _sys_nerr)
     {
+      /* Buffer we use to print the number in.  For a maximum size for
+	 `int' of 8 bytes we never need more than 20 digits.  */
+      char numbuf[21];
       const char *unk = _("Unknown error ");
       const size_t unklen = strlen (unk);
-      char *p = buf + buflen;
-      *--p = '\0';
-      p = _itoa_word (errnum, p, 10, 0);
-      return memcpy (p - unklen, unk, unklen);
+      char *p, *q;
+
+      numbuf[20] = '\0';
+      p = _itoa_word (errnum, &numbuf[20], 10, 0);
+
+      /* Now construct the result while taking care for the destination
+	 buffer size.  */
+      q = __mempcpy (buf, unk, MIN (unklen, buflen));
+      if (unklen < buflen)
+	__stpncpy (q, p, buflen - unklen);
+
+      /* Terminate the string in any case.  */
+      if (buflen > 0)
+	buf[buflen - 1] = '\0';
+
+      return buf;
     }
 
   return (char *) _(_sys_errlist[errnum]);