Mercurial > hg > octave-lojdl > gnulib-hg
changeset 14852:b88eb8c00c28
perror: call strerror_r directly
No need to make a wrapper that burns static storage when we can
just use stack storage.
* modules/perror (Files): Drop strerror-impl.h.
* lib/perror.c (perror): Use our own stack buffer, rather than
calling a wrapper that uses static storage.
* doc/posix-functions/perror.texi (perror): Document a limitation
of our replacement.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Tue, 24 May 2011 14:27:04 -0600 |
parents | e9cc9d33a1b8 |
children | bdc85db78f47 |
files | ChangeLog doc/posix-functions/perror.texi lib/perror.c modules/perror |
diffstat | 4 files changed, 22 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2011-06-01 Eric Blake <eblake@redhat.com> + perror: call strerror_r directly + * modules/perror (Files): Drop strerror-impl.h. + * lib/perror.c (perror): Use our own stack buffer, rather than + calling a wrapper that uses static storage. + * doc/posix-functions/perror.texi (perror): Document a limitation + of our replacement. + strerror_r: fix includes for FreeBSD * lib/strerror_r.c (includes): Use <stdlib.h> unconditionally, since we use abort on some platforms.
--- a/doc/posix-functions/perror.texi +++ b/doc/posix-functions/perror.texi @@ -24,4 +24,8 @@ POSIX requires that this function set the stream error bit (detected by @code{ferror}) on write failure, but not all platforms do this: glibc 2.13. +@item +POSIX requires that this function not alter stream orientation, but +the gnulib replacement locks in byte orientation and fails on wide +character streams. @end itemize
--- a/lib/perror.c +++ b/lib/perror.c @@ -40,10 +40,18 @@ void perror (const char *string) { - const char *errno_description = my_strerror (errno); + char stackbuf[256]; + int ret; + + /* Our implementation guarantees that this will be a non-empty + string, even if it returns EINVAL; and stackbuf should be sized + large enough to avoid ERANGE. */ + ret = strerror_r (errno, stackbuf, sizeof stackbuf); + if (ret == ERANGE) + abort (); if (string != NULL && *string != '\0') - fprintf (stderr, "%s: %s\n", string, errno_description); + fprintf (stderr, "%s: %s\n", string, stackbuf); else - fprintf (stderr, "%s\n", errno_description); + fprintf (stderr, "%s\n", stackbuf); }