bug-gnulib
[Top][All Lists]
Advanced

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

new module 'xstrerror'


From: Bruno Haible
Subject: new module 'xstrerror'
Date: Thu, 05 Oct 2023 22:29:00 +0200

Looking for a function which produces the internationalized description of
an errno value, with the two additional requirements that
  - it should be multithread-safe,
  - it should be easy to use,
I didn't find any answer in POSIX or gnulib.

https://man7.org/linux/man-pages/man3/strerror.3.html lists a few functions,
but:
  - strerror is not multithread-safe,
  - strerrordesc_np returns the English description, not internationalized,
  - strerror_l requires a locale_t object and thus does not work for the
    current locale,
  - strerror_r is not easy to use, because
      . it requires the caller to provide a preallocated buffer,
      . it comes in two variants (POSIX and GNU).

I considered adding a function xstrerror that would return the string
from strerror_r in a per-thread static buffer. But this return convention
has a pitfall: if the returned pointer ever gets passed to a different
thread, value corruption will occur, that is hard to detect and to debug.
(glibc's inet_ntoa function uses this return convention; but that does not
make it any more reliable.)

So, the best approach is still a function that returns the value in storage
with indefinite extent, namely malloc()ed.

I picked a 2-arguments function, rather than a 1-argument function, because
that's more useful in practice.

There's a potential conflict with the 1-argument function named xstrerror
from libiberty (GCC/binutils/gdb). The migration is simple:
  xstrerror_libiberty (errnum) == xstrerror_gnulib (NULL, errnum).


2023-10-05  Bruno Haible  <bruno@clisp.org>

        xstrerror: Add tests.
        * tests/test-xstrerror.c: New file.
        * modules/xstrerror-tests: New file.

        xstrerror: New module.
        * lib/xstrerror.h: New file.
        * lib/xstrerror.c: New file.
        * modules/xstrerror: New file.
        * modules/strerror_r-posix (configure.ac): Update comment.

Attachment: 0001-xstrerror-New-module.patch
Description: Text Data

Attachment: 0002-xstrerror-Add-tests.patch
Description: Text Data


reply via email to

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