changeset 14778:85c9ed97c132

perror: work around FreeBSD bug POSIX requires that 'errno = 0; perror ("")' print the same message as strerror(0), but this failed if we were replacing strerror to work around the FreeBSD bug of treating 0 as a failure. The goal here is to _not_ replace perror on glibc, even though strerror_r has to be replaced, because the strerror_r replacement is only for the sake of correcting the signature rather than working around bugs in the handling of any particular errnum value. Recall that $gl_cv_func_strerror_r_works is only set if the POSIX signature was detected in the first place. * m4/perror.m4 (gl_FUNC_PERROR): Also replace perror if strerror_r is broken. Move AC_LIBOBJ... * modules/perror (configure.ac): Here. * doc/posix-functions/perror.texi (perror): Document this. * tests/test-perror2.c (main): Enhance test. Signed-off-by: Eric Blake <eblake@redhat.com>
author Eric Blake <eblake@redhat.com>
date Fri, 20 May 2011 11:09:53 -0600
parents d614bf2c2613
children 70726a18c2ed
files ChangeLog doc/posix-functions/perror.texi m4/perror.m4 modules/perror tests/test-perror2.c
diffstat 5 files changed, 44 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2011-05-20  Eric Blake  <eblake@redhat.com>
 
+	perror: work around FreeBSD bug
+	* m4/perror.m4 (gl_FUNC_PERROR): Also replace perror if strerror_r
+	is broken.  Move AC_LIBOBJ...
+	* modules/perror (configure.ac): Here.
+	* doc/posix-functions/perror.texi (perror): Document this.
+	* tests/test-perror2.c (main): Enhance test.
+
 	test-perror: check for strerror interactions
 	* tests/macros.h (STREQ) Add macro.
 	* modules/perror-tests (Files): Add second test.
--- a/doc/posix-functions/perror.texi
+++ b/doc/posix-functions/perror.texi
@@ -12,6 +12,10 @@
 This function does not support the error values that are specified by POSIX
 but not defined by the system, on some platforms:
 OpenBSD 4.0, OSF/1 5.1, Cygwin 1.5.x, mingw.
+@item
+This function treats @code{errno} of 0 like failure, although POSIX
+requires that the message declare it as a success, on some platforms:
+FreeBSD 8.2
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- a/m4/perror.m4
+++ b/m4/perror.m4
@@ -1,4 +1,4 @@
-# perror.m4 serial 1
+# perror.m4 serial 2
 dnl Copyright (C) 2008-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,10 +8,16 @@
 [
   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
   AC_REQUIRE([gl_HEADER_ERRNO_H])
+  AC_REQUIRE([gl_FUNC_STRERROR_R])
   if test -n "$ERRNO_H"; then
     dnl The system's perror() cannot know about the new errno values we add
     dnl to <errno.h>. Replace it.
     REPLACE_PERROR=1
-    AC_LIBOBJ([perror])
   fi
+  case ${gl_cv_func_strerror_r_works-unset} in
+    unset|*yes) ;;
+    *) dnl The system's perror() probably inherits the bugs in the
+       dnl system's strerror_r(). Replace it.
+      REPLACE_PERROR=1 ;;
+  esac
 ])
--- a/modules/perror
+++ b/modules/perror
@@ -16,6 +16,9 @@
 configure.ac:
 gl_FUNC_PERROR
 gl_STRING_MODULE_INDICATOR([perror])
+if test $REPLACE_PERROR = 1; then
+  AC_LIBOBJ([perror])
+fi
 
 Makefile.am:
 
--- a/tests/test-perror2.c
+++ b/tests/test-perror2.c
@@ -88,6 +88,28 @@
     ASSERT (STREQ (msg4, str4));
   }
 
+  /* Test that perror uses the same message as strerror.  */
+  {
+    int errs[] = { EACCES, 0, -3, };
+    int i;
+    for (i = 0; i < SIZEOF (errs); i++)
+      {
+        char buf[256];
+        char *err = strerror (errs[i]);
+
+        ASSERT (err);
+        ASSERT (strlen (err) < sizeof buf);
+        rewind (stderr);
+        ASSERT (ftruncate (fileno (stderr), 0) == 0);
+        errno = errs[i];
+        perror (NULL);
+        ASSERT (!ferror (stderr));
+        rewind (stderr);
+        ASSERT (fgets (buf, sizeof buf, stderr) == buf);
+        ASSERT (strstr (buf, err));
+      }
+  }
+
   /* Test that perror reports write failure.  */
   {
     ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);