]> Savannah Git Hosting - gnulib.git/commitdiff
strerror_r-posix: Fix for Hurd.
authorBruno Haible <bruno@clisp.org>
Fri, 2 Sep 2022 21:45:21 +0000 (23:45 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 2 Sep 2022 23:03:15 +0000 (01:03 +0200)
* lib/strerror_r.c (strerror_r): Interpret return value of
__xpg_strerror_r correctly. Remove assumption about how strerror_r
behaves.

ChangeLog
lib/strerror_r.c

index 5c0b13d40d8c42025af6c4cc9fb7036b44d6d34c..31f08748fb31c45d608b859797320d9d0fd7358d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2022-09-02  Bruno Haible  <bruno@clisp.org>
+
+       strerror_r-posix: Fix for Hurd.
+       * lib/strerror_r.c (strerror_r): Interpret return value of
+       __xpg_strerror_r correctly. Remove assumption about how strerror_r
+       behaves.
+
 2022-09-02  Bruno Haible  <bruno@clisp.org>
 
        ptsname tests, ptsname_r tests: Fix test failures on Hurd.
index 85a218935c679567a30c42a1c303879b4634a423..b154c292b48946303a3eb957c49c905046415034 100644 (file)
@@ -166,16 +166,19 @@ strerror_r (int errnum, char *buf, size_t buflen)
 
 # if HAVE___XPG_STRERROR_R
       ret = __xpg_strerror_r (errnum, buf, buflen);
-      if (ret < 0)
-        ret = errno;
+      /* ret is 0 upon success, or EINVAL or ERANGE upon failure.  */
 # endif
 
       if (!*buf)
         {
-          /* glibc 2.13 would not touch buf on err, so we have to fall
-             back to GNU strerror_r which always returns a thread-safe
-             untruncated string to (partially) copy into our buf.  */
-          char *errstring = strerror_r (errnum, buf, buflen);
+          /* glibc 2.13 ... 2.34 (at least) don't touch buf upon failure.
+             Therefore we have to fall back to strerror_r which, for valid
+             errnum, returns a thread-safe untruncated string.  For invalid
+             errnum, though, it returns a truncated string, which does not
+             allow us to determine whether to return ERANGE or 0.  Thus we
+             need to pass a sufficiently large buffer.  */
+          char stackbuf[80];
+          char *errstring = strerror_r (errnum, stackbuf, sizeof stackbuf);
           ret = errstring ? safe_copy (buf, buflen, errstring) : errno;
         }
     }