changeset 16124:c56792ef17f5

getcwd: Fix bug from 2011-08-17. * m4/getcwd.m4 (gl_FUNC_GETCWD): Set REPLACE_GETCWD to 1 only on platforms that need it. * m4/getcwd-abort-bug.m4 (gl_FUNC_GETCWD_ABORT_BUG): Consider a return code of 4 to be a failure, not a success. This ensures that REPLACE_GETCWD becomes 1 on OpenBSD 4.9 and NetBSD 5.1.
author Bruno Haible <bruno@clisp.org>
date Mon, 28 Nov 2011 00:04:12 +0100
parents 25b04be44d9e
children df1c78661f1f
files ChangeLog m4/getcwd-abort-bug.m4 m4/getcwd.m4
diffstat 3 files changed, 31 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2011-11-27  Bruno Haible  <bruno@clisp.org>
+
+	getcwd: Fix bug from 2011-08-17.
+	* m4/getcwd.m4 (gl_FUNC_GETCWD): Set REPLACE_GETCWD to 1 only on
+	platforms that need it.
+	* m4/getcwd-abort-bug.m4 (gl_FUNC_GETCWD_ABORT_BUG): Consider a return
+	code of 4 to be a failure, not a success. This ensures that
+	REPLACE_GETCWD becomes 1 on OpenBSD 4.9 and NetBSD 5.1.
+
 2011-11-27  Bruno Haible  <bruno@clisp.org>
 
 	binary-io tests: Avoid test failure on mingw when libtool is used.
--- a/m4/getcwd-abort-bug.m4
+++ b/m4/getcwd-abort-bug.m4
@@ -1,4 +1,4 @@
-# serial 6
+# serial 7
 # Determine whether getcwd aborts when the length of the working directory
 # name is unusually large.  Any length between 4k and 16k trigger the bug
 # when using glibc-2.4.90-9 or older.
@@ -91,7 +91,8 @@
      results in a failed assertion.  */
   cwd = getcwd (NULL, 0);
   if (cwd == NULL)
-    fail = 4; /* getcwd failed.  This is ok, and expected.  */
+    fail = 4; /* getcwd failed: it refuses to return a string longer
+                 than PATH_MAX.  */
   free (cwd);
 
   /* Call rmdir first, in case the above chdir failed.  */
@@ -110,11 +111,18 @@
 }
           ]])],
     [gl_cv_func_getcwd_abort_bug=no],
-    dnl A "regular" nonzero return does not indicate this bug.
-    dnl An abort will provoke an exit code of something like 134 (128 + 6).
-    [test $? -gt 128 \
-      && gl_cv_func_getcwd_abort_bug=yes \
-      || gl_cv_func_getcwd_abort_bug=no],
+    [dnl An abort will provoke an exit code of something like 134 (128 + 6).
+     dnl An exit code of 4 can also occur (in OpenBSD 4.9, NetBSD 5.1 for
+     dnl example): getcwd (NULL, 0) fails rather than returning a string
+     dnl longer than PATH_MAX.  This may be POSIX compliant (in some
+     dnl interpretations of POSIX).  But gnulib's getcwd module wants to
+     dnl provide a non-NULL value in this case.
+     ret=$?
+     if test $ret -ge 128 || test $ret = 4; then
+       gl_cv_func_getcwd_abort_bug=yes
+     else
+       gl_cv_func_getcwd_abort_bug=no
+     fi],
     [gl_cv_func_getcwd_abort_bug=yes])
   ])
   AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])
--- a/m4/getcwd.m4
+++ b/m4/getcwd.m4
@@ -6,7 +6,7 @@
 # with or without modifications, as long as this notice is preserved.
 
 # Written by Paul Eggert.
-# serial 10
+# serial 11
 
 AC_DEFUN([gl_FUNC_GETCWD_NULL],
   [
@@ -139,12 +139,12 @@
       ;;
   esac
 
-  case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature$gl_cv_func_getcwd_path_max,$gl_abort_bug in
-  *yes,yes,yes,no) ;;
-  *)
-    dnl Full replacement lib/getcwd.c, overrides LGPL replacement.
-    REPLACE_GETCWD=1;;
-  esac
+  if { case "$gl_cv_func_getcwd_null" in *yes) false;; *) true;; esac; } \
+     || test $gl_cv_func_getcwd_posix_signature != yes \
+     || test "$gl_cv_func_getcwd_path_max" != yes \
+     || test $gl_abort_bug = yes; then
+    REPLACE_GETCWD=1
+  fi
 ])
 
 # Prerequisites of lib/getcwd.c, when full replacement is in effect.