public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
Subject: Re: [PATCH 13/13] string: Add errname_np and errdesc_np
Date: Thu, 28 May 2020 14:28:54 +0200	[thread overview]
Message-ID: <875zcgckft.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <20200519180518.318733-14-adhemerval.zanella@linaro.org> (Adhemerval Zanella via Libc-alpha's message of "Tue, 19 May 2020 15:05:18 -0300")

* Adhemerval Zanella via Libc-alpha:

> The errname_np returns error number name (i.g. "EINVAL" for EINVAL)
> while errdescr_np returns string describing error number (i.g
> "Invalid argument" for EINVAL).  Different than strerror, errdescr_np
> does not attempt to translate the return description and both functions
> return NULL for an invalid error number.

If you use a name that starts with "str", it's in the implementation
namespace, and you don't need the _np suffix.

Is errdescr_np really that useful?  If yes, maybe we should define a
global C locale object, similar to LC_GLOBAL_LOCALE, so that programmers
can simply use strerror_l for this instead (except that it won't be
async-signal-safe, hmm)?

> diff --git a/manual/errno.texi b/manual/errno.texi
> index 8cb4ce8b48..97ba3916ed 100644
> --- a/manual/errno.texi
> +++ b/manual/errno.texi
> @@ -1207,6 +1207,28 @@ to @code{errno}.
>  The function @code{perror} is declared in @file{stdio.h}.
>  @end deftypefun
>  
> +@deftypefun void errname_np (int @var{errnum})
> +@standards{GNU, string.h}
> +@safety{@prelim{}@mtsafe{@mtssigintr{}}@assafe{}@acsafe{}}
> +This function returns the name describing the error @var{errnum} or
> +@code{NULL} for invalid error number (i.g "EINVAL" for @code{EINVAL}).
> +
> +@pindex string.h
> +This function is a GNU extension, declared in the header file @file{string.h}.
> +@end deftypefun
> +
> +@deftypefun void errdesc_np (int @var{errnum})
> +@standards{GNU, string.h}
> +@safety{@prelim{}@mtsafe{@mtssigintr{}}@assafe{}@acsafe{}}
> +This function returns the message describing the error @var{errnum} or
> +@code{NULL} for invalid error number (i.g Invalid argument" for
> +@code{EINVAL}).  Different than @code{strerror} the returned description is
> +not translated.
> +
> +@pindex string.h
> +This function is a GNU extension, declared in the header file @file{string.h}.
> +@end deftypefun

Why @mtssigintr{} (twice)?

I think error numbers for which no constant exist are still valid (in
the C++ sense), so maybe we should phrase this differently: These
functions return NULL if there is no known E* constant with this value.

> diff --git a/stdio-common/errlist.c b/stdio-common/errlist.c
> index f7a49fbf35..dd078143f2 100644
> --- a/stdio-common/errlist.c
> +++ b/stdio-common/errlist.c
> @@ -28,4 +28,12 @@ const char *const _sys_errlist_internal[] =
>  
>  const int _sys_nerr_internal = array_length (_sys_errlist_internal);
>  
> +const char *const _sys_errname_internal[] =
> +  {
> +/* This file is auto-generated from errlist.def.  */
> +#include <errlist-name.h>
> +  };
> +
> +const int _sys_nname_internal = array_length (_sys_errname_internal);
> +
>  #include <errlist-compat.c>

There should not be a reason to emit a separate constant for the length
to the data segment.  And we should eliminate the extra relocations this
array adds.  But I think we can add these changes later as an
optimization.

> diff --git a/stdio-common/test-err_np.c b/stdio-common/test-err_np.c
> new file mode 100644
> index 0000000000..b463c9f3ef
> --- /dev/null
> +++ b/stdio-common/test-err_np.c

I would expect this test to compare against strerror and the E*
constants directly.  For the latter, we should generate a table
containing elements like:

  { EINVAL, "EINVAL" },
  
> diff --git a/string/_strerror.c b/string/_strerror.c
> index 6ae8d47f0f..8bb9bfeaa9 100644
> --- a/string/_strerror.c
> +++ b/string/_strerror.c
> @@ -24,14 +24,14 @@
>  char *
>  __strerror_r (int errnum, char *buf, size_t buflen)
>  {
> -  if (__glibc_unlikely (errnum < 0 || errnum >= _sys_nerr_internal
> -			|| _sys_errlist_internal[errnum] == NULL))
> +  const char *r = __errdescr_np (errnum);
> +  if (r == NULL)
>      {
>        __snprintf (buf, buflen, "%s%d", _("Unknown error "), errnum);
>        return buf;
>      }
>  
> -  return (char *) _(_sys_errlist_internal[errnum]);
> +  return _(_sys_errlist_internal[errnum]);

Shouldn't the last line re-use r?

> diff --git a/string/errname_np.c b/string/errname_np.c
> new file mode 100644
> index 0000000000..61455da219
> --- /dev/null
> +++ b/string/errname_np.c

> +const char *const
> +errname_np (int errnum)
> +{
> +  const char *name = NULL;
> +
> +  if (errnum >= 0 && errnum < _sys_nname_internal)
> +    name = _sys_errname_internal[errnum];
> +
> +  return name;
> +}

I'm surprised that index 0 is looked up in the table.

It may make sense to move the definition of the array out ouf errlist.c,
so that it can be shared across all ports.

Thanks,
Florian


  reply	other threads:[~2020-05-28 12:29 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 ` [PATCH 08/13] string: Simplify strerror_r Adhemerval Zanella
2020-05-28 11:56   ` 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 [this message]
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=875zcgckft.fsf@oldenburg2.str.redhat.com \
    --to=fweimer@redhat.com \
    --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).