bug-gnulib
[Top][All Lists]
Advanced

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

[PATCHv2 2/2] strerror_r: fix OpenBSD behavior on out-of-range


From: Eric Blake
Subject: [PATCHv2 2/2] strerror_r: fix OpenBSD behavior on out-of-range
Date: Tue, 21 Jun 2011 11:08:15 -0600

On OpenBSD, strerror_r(1000,buf,19) gives "Unknown error: " rather
than "Unknown error: 100" while failing with ERANGE.  Admittedly,
this behavior is nice, since a truncated integer is misleading,
but all other platforms use maximal strings on ERANGE and we are
already replacing strerror_r for other reasons, so it is easier
to work around this behavior than to adjust the testsuite (how
do you quickly decide if the only reason that the ERANGE string
was shorter than maximal was because the implementation avoided
truncating an integer?).

This patch intentionally avoids dragging in the strnlen module.

* lib/strerror_r.c (strerror_r): Always use maximal string.
* doc/posix-functions/strerror_r.texi (strerror_r): Document it.

Signed-off-by: Eric Blake <address@hidden>
---

On the other hand, I noticed we were already using maximal strings
for Solaris, so I consolidated this code to the existing code
and pushed.  We can change our minds later if we want to instead
relax the test to permit OpenBSD's avoidance of a truncated int.

 ChangeLog                           |    4 ++++
 doc/posix-functions/strerror_r.texi |    5 +++++
 lib/strerror_r.c                    |   32 +++++++++++++++++++-------------
 3 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 70c1903..07f3819 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2011-06-21  Eric Blake  <address@hidden>

+       strerror_r: fix OpenBSD behavior on out-of-range
+       * lib/strerror_r.c (strerror_r): Always use maximal string.
+       * doc/posix-functions/strerror_r.texi (strerror_r): Document it.
+
        strerror_r: fix OpenBSD behavior on 0
        * lib/strerror-override.c (strerror_override): Also override 0
        when needed.
diff --git a/doc/posix-functions/strerror_r.texi 
b/doc/posix-functions/strerror_r.texi
index 5aaa1c6..08b59ec 100644
--- a/doc/posix-functions/strerror_r.texi
+++ b/doc/posix-functions/strerror_r.texi
@@ -64,6 +64,11 @@ strerror_r
 function fails to leave a NUL-terminated string in the buffer on some
 platforms:
 glibc 2.13, FreeBSD 8.2, Solaris 10.
address@hidden
+When the value is out of range but the buffer is too small, this
+function does not always return the longest possible string on some
+platforms:
+OpenBSD 4.7.
 @end itemize

 Portability problems not fixed by Gnulib:
diff --git a/lib/strerror_r.c b/lib/strerror_r.c
index 0da9ba2..2b3f1f2 100644
--- a/lib/strerror_r.c
+++ b/lib/strerror_r.c
@@ -175,17 +175,11 @@ strerror_r (int errnum, char *buf, size_t buflen)
         ret = strerror_r (errnum, buf, buflen);
     }
 # else
-    /* Solaris 10 does not populate buf on ERANGE.  */
     ret = strerror_r (errnum, buf, buflen);
-    if (ret == ERANGE && !*buf)
-      {
-        char stackbuf[STACKBUF_LEN];

-        if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
-          /* STACKBUF_LEN should have been large enough.  */
-          abort ();
-        safe_copy (buf, buflen, stackbuf);
-      }
+    /* Some old implementations may return (-1, EINVAL) instead of EINVAL.  */
+    if (ret < 0)
+      ret = errno;
 # endif

 # ifdef _AIX
@@ -203,11 +197,23 @@ strerror_r (int errnum, char *buf, size_t buflen)
         if (buflen <= len)
           ret = ERANGE;
       }
-# endif
+# else
+    /* Solaris 10 does not populate buf on ERANGE.  OpenBSD 4.7
+       truncates early on ERANGE rather than return a partial integer.
+       We prefer the maximal string.  We set buf[0] earlier, and we
+       know of no implementation that modifies buf to be an
+       unterminated string, so this strlen should be portable in
+       practice (rather than pulling in a safer strnlen).  */
+    if (ret == ERANGE && strlen (buf) < buflen - 1)
+      {
+        char stackbuf[STACKBUF_LEN];

-    /* Some old implementations may return (-1, EINVAL) instead of EINVAL.  */
-    if (ret < 0)
-      ret = errno;
+        /* STACKBUF_LEN should have been large enough.  */
+        if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
+          abort ();
+        safe_copy (buf, buflen, stackbuf);
+      }
+# endif

 #else /* USE_SYSTEM_STRERROR */

-- 
1.7.4.4




reply via email to

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