From: Eric Blake Date: Mon, 6 Jun 2011 21:16:26 +0000 (-0600) Subject: strerror: work around FreeBSD bug X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c91b77e3aa7c73244c8f89145620ea168811bba;p=pspp strerror: work around FreeBSD bug Breaking strerror away from strerror_r re-exposed the FreeBSD strerror(0) bug. * lib/strerror.c (strerror): Special case 0. Reported by Bruno Haible. Signed-off-by: Eric Blake --- diff --git a/ChangeLog b/ChangeLog index 363ee1f01e..35497d0dfa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2011-06-06 Eric Blake + strerror: work around FreeBSD bug + * lib/strerror.c (strerror): Special case 0. + Reported by Bruno Haible. + strerror-override: avoid bloating errno module * modules/errno (Files, configure.ac): Move replacement strings... * modules/strerror-override: ...to new module. diff --git a/lib/strerror.c b/lib/strerror.c index 8c411799a0..4dc0b65c1e 100644 --- a/lib/strerror.c +++ b/lib/strerror.c @@ -45,10 +45,22 @@ strerror (int n) if (msg) return (char *) msg; + /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */ + if (n) + msg = strerror (n); + else + { + int saved_errno = errno; + errno = 0; + msg = strerror (n); + if (errno) + msg = "Success"; + errno = saved_errno; + } + /* Our strerror_r implementation might use the system's strerror buffer, so all other clients of strerror have to see the error copied into a buffer that we manage. */ - msg = strerror (n); if (!msg || !*msg) { static char const fmt[] = "Unknown error %d";