>From 411ad1157dd0d9ac9bafb545f03c3b5ae15cd22c Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 29 Oct 2017 17:33:22 +0100 Subject: [PATCH 6/7] strerror_r-posix: Fix behaviour and test failure on Haiku. * lib/strerror_r.c (strerror_r): Don't assume that valid error numbers are positive. Work around return value 0 instead of ERANGE on Haiku. For unknown error numbers, use a format string consistent with perror(). * doc/posix-functions/strerror_r.texi: Mention the Haiku problem. * tests/test-strerror_r.c (main): Don't assume that valid error numbers are positive. --- ChangeLog | 10 ++++++++++ doc/posix-functions/strerror_r.texi | 2 +- lib/strerror_r.c | 18 ++++++++++++++---- tests/test-strerror_r.c | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 66d0ef6..8539105 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2017-10-29 Bruno Haible + strerror_r-posix: Fix behaviour and test failure on Haiku. + * lib/strerror_r.c (strerror_r): Don't assume that valid error numbers + are positive. Work around return value 0 instead of ERANGE on Haiku. + For unknown error numbers, use a format string consistent with perror(). + * doc/posix-functions/strerror_r.texi: Mention the Haiku problem. + * tests/test-strerror_r.c (main): Don't assume that valid error numbers + are positive. + +2017-10-29 Bruno Haible + get-rusage-data: Avoid crash on Haiku. * lib/get-rusage-data.c: Avoid the setlimit-based implementation. diff --git a/doc/posix-functions/strerror_r.texi b/doc/posix-functions/strerror_r.texi index 40a9465..220446a 100644 --- a/doc/posix-functions/strerror_r.texi +++ b/doc/posix-functions/strerror_r.texi @@ -62,7 +62,7 @@ HP-UX 11.31. When the buffer is too small and the value is in range, this function does not fail, but instead truncates the result and returns 0 on some platforms: -AIX 6.1, OSF/1 5.1. +AIX 6.1, OSF/1 5.1, Haiku 2017. @item When the value is not in range or the buffer is too small, this function fails to leave a NUL-terminated string in the buffer on some diff --git a/lib/strerror_r.c b/lib/strerror_r.c index e3a1c29..14e3195 100644 --- a/lib/strerror_r.c +++ b/lib/strerror_r.c @@ -212,13 +212,16 @@ strerror_r (int errnum, char *buf, size_t buflen) # else ret = strerror_r (errnum, buf, buflen); - /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */ + /* Some old implementations may return (-1, EINVAL) instead of EINVAL. + But on Haiku, valid error numbers are negative. */ +# if !defined __HAIKU__ if (ret < 0) ret = errno; +# endif # endif -# ifdef _AIX - /* AIX returns 0 rather than ERANGE when truncating strings; try +# if defined _AIX || defined __HAIKU__ + /* AIX and Haiku return 0 rather than ERANGE when truncating strings; try again until we are sure we got the entire string. */ if (!ret && strlen (buf) == buflen - 1) { @@ -442,7 +445,14 @@ strerror_r (int errnum, char *buf, size_t buflen) #endif if (ret == EINVAL && !*buf) - snprintf (buf, buflen, "Unknown error %d", errnum); + { +#if defined __HAIKU__ + /* For consistency with perror(). */ + snprintf (buf, buflen, "Unknown Application Error (%d)", errnum); +#else + snprintf (buf, buflen, "Unknown error %d", errnum); +#endif + } errno = saved_errno; return ret; diff --git a/tests/test-strerror_r.c b/tests/test-strerror_r.c index 16f99df..7a06ed6 100644 --- a/tests/test-strerror_r.c +++ b/tests/test-strerror_r.c @@ -108,7 +108,7 @@ main (void) errno = 0; ret = strerror_r (err, buf, i); ASSERT (errno == 0); - if (err < 0) + if (j == 2) ASSERT (ret == ERANGE || ret == EINVAL); else ASSERT (ret == ERANGE); -- 2.7.4