X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fstrerror_r.c;h=2b3f1f267e2a671019b5e2709aa9d5c42aa47991;hb=c3de829c6f94be07b6104c5403d070791fc516fb;hp=d0c7be953e3a05cbc171672e49f1576ed819d148;hpb=23b18247253c80345d5fcf7173c5b734b0b8f434;p=gnulib.git diff --git a/lib/strerror_r.c b/lib/strerror_r.c index d0c7be953..2b3f1f267 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,15 +197,23 @@ strerror_r (int errnum, char *buf, size_t buflen) if (buflen <= len) ret = ERANGE; } -# endif - - /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */ - if (ret < 0) - ret = errno; +# 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]; - /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */ - if (errnum == 0 && ret == EINVAL) - ret = safe_copy (buf, buflen, "Success"); + /* 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 */