From patchwork Tue May 19 18:05:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 39333 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 836A6393F84D; Tue, 19 May 2020 18:05:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 836A6393F84D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1589911553; bh=SskZgK4E+l8WMLNAFEgx3vzJRvaRURiJ+C3tmM+s3Os=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=GDbCavcGEz9gKNonNZoTiY3sFgjX8COuvulcdVpR3m29kjDyXbPSv55wCx6t61TnQ 3LNkukDEF32j2OCvHcvnmqzPMeW6w43tDl8altv+t2Z7CyHFcdLmXxROkEmiM0v2QL sK1Yt7l6ViPp+TMn3hLueWLubCyMkPSjJ6Jm21zY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x744.google.com (mail-qk1-x744.google.com [IPv6:2607:f8b0:4864:20::744]) by sourceware.org (Postfix) with ESMTPS id 2398F395A01E for ; Tue, 19 May 2020 18:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2398F395A01E Received: by mail-qk1-x744.google.com with SMTP id g185so451388qke.7 for ; Tue, 19 May 2020 11:05:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SskZgK4E+l8WMLNAFEgx3vzJRvaRURiJ+C3tmM+s3Os=; b=JQTD+Mqf/qsB4XIp409waKnDeILRcxKYRB4l3OHcSQr00xqIe7zYUcBlNPphh4hIqQ SSf1/CJcvde2mlQUOCjSWcL+R9Li3HuW7bbuD9ypWH27pnMirdeL5IbCf+pK6SuEoLCG ytl9ihWlDtYwMfqIpIbJXrnTfNrH+7WCYI5cySwJYbBRYhdUWaL2CLTv76qyPC/86J49 XZguX3ruIUdIogTkHFhQLDzH6WfeKN3To8M/10OaeL67JoMfzsunwEPn+PAtGz36F/B8 TgHMdr9HgsjMpwJngDGS1MoX3EtHIqyxhcIR+BrDvju6vf4OEJK9iICCz5CJahVR5xrS z8Tg== X-Gm-Message-State: AOAM531ixVZ/zDRHkfIgZiV8NcLEBGUsAhLI+pSGxtjauEIXy5GKUzLg cM1wewu7IX1JYoDcurvD5xRlqWi0JcA= X-Google-Smtp-Source: ABdhPJy72NeUIxD2fh2JPilIy2GlEYmqJu2rBCgFFOJ5Un/d1PYXxuMjyMp+n6h3AuCCpAPjzxxLug== X-Received: by 2002:ae9:e215:: with SMTP id c21mr48501qkc.259.1589911548311; Tue, 19 May 2020 11:05:48 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id m6sm230091qkc.4.2020.05.19.11.05.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2020 11:05:48 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 08/13] string: Simplify strerror_r Date: Tue, 19 May 2020 15:05:13 -0300 Message-Id: <20200519180518.318733-9-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200519180518.318733-1-adhemerval.zanella@linaro.org> References: <20200519180518.318733-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-16.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 . */ +#include #include -#include #include #include -#include -#include -#include <_itoa.h> - -/* It is critical here that we always use the `dcgettext' function for - the message translation. Since only defines the macro - `dgettext' to use `dcgettext' for optimizing programs this is not - always guaranteed. */ -#ifndef dgettext -# include /* 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 . */ +#include #include #include #include #include #include -#include -#include <_itoa.h> - -/* It is critical here that we always use the `dcgettext' function for - the message translation. Since only defines the macro - `dgettext' to use `dcgettext' for optimizing programs this is not - always guaranteed. */ -#ifndef dgettext -# include /* 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; }