public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 08/13] string: Simplify strerror_r
Date: Tue, 19 May 2020 15:05:13 -0300	[thread overview]
Message-ID: <20200519180518.318733-9-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20200519180518.318733-1-adhemerval.zanella@linaro.org>

Use snprintf instead of mempcpy plus itoa_word and remove unused
definitions.

Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
and s390x-linux-gnu.
---
 string/_strerror.c       | 41 ++----------------------
 sysdeps/mach/_strerror.c | 67 +++-------------------------------------
 2 files changed, 7 insertions(+), 101 deletions(-)

diff --git a/string/_strerror.c b/string/_strerror.c
index 985fd4e3c6..6ae8d47f0f 100644
--- a/string/_strerror.c
+++ b/string/_strerror.c
@@ -15,22 +15,10 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <string.h>
 #include <libintl.h>
-#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-#include <sys/param.h>
-#include <_itoa.h>
-
-/* It is critical here that we always use the `dcgettext' function for
-   the message translation.  Since <libintl.h> only defines the macro
-   `dgettext' to use `dcgettext' for optimizing programs this is not
-   always guaranteed.  */
-#ifndef dgettext
-# include <locale.h>		/* We need LC_MESSAGES.  */
-# define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
-#endif
 
 /* Return a string describing the errno code in ERRNUM.  */
 char *
@@ -39,32 +27,7 @@ __strerror_r (int errnum, char *buf, size_t buflen)
   if (__glibc_unlikely (errnum < 0 || errnum >= _sys_nerr_internal
 			|| _sys_errlist_internal[errnum] == NULL))
     {
-      /* 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 ");
-      size_t unklen = strlen (unk);
-      char *p, *q;
-      bool negative = errnum < 0;
-
-      numbuf[20] = '\0';
-      p = _itoa_word (abs (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 (negative && unklen < buflen)
-	{
-	  *q++ = '-';
-	  ++unklen;
-	}
-      if (unklen < buflen)
-	memcpy (q, p, MIN ((size_t) (&numbuf[21] - p), buflen - unklen));
-
-      /* Terminate the string in any case.  */
-      if (buflen > 0)
-	buf[buflen - 1] = '\0';
-
+      __snprintf (buf, buflen, "%s%d", _("Unknown error "), errnum);
       return buf;
     }
 
diff --git a/sysdeps/mach/_strerror.c b/sysdeps/mach/_strerror.c
index d932b1bd58..399de330d9 100644
--- a/sysdeps/mach/_strerror.c
+++ b/sysdeps/mach/_strerror.c
@@ -15,22 +15,12 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <string.h>
 #include <libintl.h>
 #include <stdio.h>
 #include <string.h>
 #include <mach/error.h>
 #include <errorlib.h>
-#include <sys/param.h>
-#include <_itoa.h>
-
-/* It is critical here that we always use the `dcgettext' function for
-   the message translation.  Since <libintl.h> only defines the macro
-   `dgettext' to use `dcgettext' for optimizing programs this is not
-   always guaranteed.  */
-#ifndef dgettext
-# include <locale.h>		/* We need LC_MESSAGES.  */
-# define dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
-#endif
 
 /* Return a string describing the errno code in ERRNUM.  */
 char *
@@ -50,26 +40,8 @@ __strerror_r (int errnum, char *buf, size_t buflen)
 
   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
     {
-      /* 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 = _("Error in unknown error system: ");
-      const size_t unklen = strlen (unk);
-      char *p, *q;
-
-      numbuf[20] = '\0';
-      p = _itoa_word (errnum, &numbuf[20], 16, 1);
-
-      /* Now construct the result while taking care for the destination
-	 buffer size.  */
-      q = __mempcpy (buf, unk, MIN (unklen, buflen));
-      if (unklen < buflen)
-	memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen));
-
-      /* Terminate the string in any case.  */
-      if (buflen > 0)
-	buf[buflen - 1] = '\0';
-
+      __snprintf (buf, buflen, "%s: %d", _("Error in unknown error system: "),
+		  errnum);
       return buf;
     }
 
@@ -80,37 +52,8 @@ __strerror_r (int errnum, char *buf, size_t buflen)
 
   if (code >= es->subsystem[sub].max_code)
     {
-      /* 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, *q;
-      size_t len = strlen (es->subsystem[sub].subsys_name);
-
-      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)
-	{
-	  q = __mempcpy (q, es->subsystem[sub].subsys_name,
-			 MIN (len, buflen - unklen));
-	  if (unklen + len < buflen)
-	    {
-	      *q++ = ' ';
-	      if (unklen + len + 1 < buflen)
-		memcpy (q, p,
-			MIN (&numbuf[21] - p, buflen - unklen - len - 1));
-	    }
-	}
-
-       /* Terminate the string in any case.  */
-      if (buflen > 0)
-	buf[buflen - 1] = '\0';
-
+      __snprintf (buf, buflen, "%s%s %d", _("Unknown error "),
+		  es->subsystem[sub].subsys_name, errnum);
       return buf;
     }
 
-- 
2.25.1


  parent reply	other threads:[~2020-05-19 18:05 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19 18:05 [PATCH 00/13] Signal and error list refactoring Adhemerval Zanella
2020-05-19 18:05 ` [PATCH v2 01/13] signal: Add signum-{generic,arch}.h Adhemerval Zanella
2020-05-19 18:05 ` [PATCH v3 02/13] signal: Move sys_siglist to a compat symbol Adhemerval Zanella
2020-05-28 14:50   ` Florian Weimer
2020-05-19 18:05 ` [PATCH v3 03/13] signal: Move sys_errlist " Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 04/13] linux: Fix __NSIG_WORDS and add __NSIG_BYTES Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 05/13] string: Remove old TLS usage on strsignal Adhemerval Zanella
2020-05-28 11:38   ` Florian Weimer
2020-06-01 18:08     ` Adhemerval Zanella
2020-06-01 18:13       ` Florian Weimer
2020-06-01 18:39         ` Adhemerval Zanella
2020-06-01 18:43           ` Florian Weimer
2020-05-19 18:05 ` [PATCH 06/13] string: Implement strerror in terms of strerror_l Adhemerval Zanella
2020-05-28 11:41   ` Florian Weimer
2020-06-01 18:28     ` Adhemerval Zanella
2020-06-03  8:24       ` Florian Weimer
2020-06-03 15:13         ` Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 07/13] string: Use tls-internal on strerror_l Adhemerval Zanella
2020-05-19 18:05 ` Adhemerval Zanella [this message]
2020-05-28 11:56   ` [PATCH 08/13] string: Simplify strerror_r Florian Weimer
2020-06-01 18:31     ` Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 09/13] string: Add strsignal test Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 10/13] string: Add strerror, strerror_r, and strerror_l test Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 11/13] string: Add strerror_l on test-strerror-errno Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 12/13] string: Add sigabbrev_np and sigdescr_np Adhemerval Zanella
2020-05-19 18:47   ` Joseph Myers
2020-05-28 12:31   ` Florian Weimer
2020-06-03 16:39     ` Adhemerval Zanella
2020-05-19 18:05 ` [PATCH 13/13] string: Add errname_np and errdesc_np Adhemerval Zanella
2020-05-28 12:28   ` Florian Weimer
2020-06-01 20:52     ` Adhemerval Zanella
2020-06-02 17:13     ` Adhemerval Zanella
2020-06-02 17:19       ` Florian Weimer
2020-06-02 17:20         ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200519180518.318733-9-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).