getcwd: break fdopendir + save_cwd recursive loop (Bug#13516)
[gnulib.git] / m4 / getcwd-abort-bug.m4
1 # serial 7
2 # Determine whether getcwd aborts when the length of the working directory
3 # name is unusually large.  Any length between 4k and 16k trigger the bug
4 # when using glibc-2.4.90-9 or older.
5
6 # Copyright (C) 2006, 2009-2013 Free Software Foundation, Inc.
7 # This file is free software; the Free Software Foundation
8 # gives unlimited permission to copy and/or distribute it,
9 # with or without modifications, as long as this notice is preserved.
10
11 # From Jim Meyering
12
13 # gl_FUNC_GETCWD_ABORT_BUG([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
14 AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
15 [
16   AC_CHECK_DECLS_ONCE([getcwd])
17   AC_CHECK_HEADERS_ONCE([unistd.h])
18   AC_REQUIRE([gl_PATHMAX_SNIPPET_PREREQ])
19   AC_CHECK_FUNCS([getpagesize])
20   AC_CACHE_CHECK([whether getcwd aborts when 4k < cwd_length < 16k],
21     gl_cv_func_getcwd_abort_bug,
22     [# Remove any remnants of a previous test.
23      rm -rf confdir-14B---
24      # Arrange for deletion of the temporary directory this test creates.
25      ac_clean_files="$ac_clean_files confdir-14B---"
26      dnl Please keep this in sync with tests/test-getcwd.c.
27      AC_RUN_IFELSE(
28        [AC_LANG_SOURCE(
29           [[
30 #include <stdlib.h>
31 #if HAVE_UNISTD_H
32 # include <unistd.h>
33 #else /* on Windows with MSVC */
34 # include <direct.h>
35 #endif
36 #include <string.h>
37 #include <sys/stat.h>
38
39 ]gl_PATHMAX_SNIPPET[
40
41 /* Don't get link errors because mkdir is redefined to rpl_mkdir.  */
42 #undef mkdir
43
44 #ifndef S_IRWXU
45 # define S_IRWXU 0700
46 #endif
47
48 /* FIXME: skip the run-test altogether on systems without getpagesize.  */
49 #if ! HAVE_GETPAGESIZE
50 # define getpagesize() 0
51 #endif
52
53 /* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
54    the 16kB pagesize on ia64 linux.  Those conditions make the code below
55    trigger a bug in glibc's getcwd implementation before 2.4.90-10.  */
56 #define TARGET_LEN (5 * 1024)
57
58 int
59 main ()
60 {
61   char *cwd;
62   size_t initial_cwd_len;
63   int fail = 0;
64
65   /* The bug is triggered when PATH_MAX < getpagesize (), so skip
66      this relatively expensive and invasive test if that's not true.  */
67 #ifdef PATH_MAX
68   int bug_possible = PATH_MAX < getpagesize ();
69 #else
70   int bug_possible = 0;
71 #endif
72   if (! bug_possible)
73     return 0;
74
75   cwd = getcwd (NULL, 0);
76   if (cwd == NULL)
77     return 2;
78
79   initial_cwd_len = strlen (cwd);
80   free (cwd);
81
82   if (1)
83     {
84       static char const dir_name[] = "confdir-14B---";
85       size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
86                               / sizeof dir_name);
87       size_t d;
88       for (d = 0; d < desired_depth; d++)
89         {
90           if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
91             {
92               if (! (errno == ERANGE || errno == ENAMETOOLONG
93                      || errno == ENOENT))
94                 fail = 3; /* Unable to construct deep hierarchy.  */
95               break;
96             }
97         }
98
99       /* If libc has the bug in question, this invocation of getcwd
100          results in a failed assertion.  */
101       cwd = getcwd (NULL, 0);
102       if (cwd == NULL)
103         fail = 4; /* getcwd didn't assert, but it failed for a long name
104                      where the answer could have been learned.  */
105       free (cwd);
106
107       /* Call rmdir first, in case the above chdir failed.  */
108       rmdir (dir_name);
109       while (0 < d--)
110         {
111           if (chdir ("..") < 0)
112             {
113               fail = 5;
114               break;
115             }
116           rmdir (dir_name);
117         }
118     }
119
120   return fail;
121 }
122           ]])],
123     [gl_cv_func_getcwd_abort_bug=no],
124     [dnl An abort will provoke an exit code of something like 134 (128 + 6).
125      dnl An exit code of 4 can also occur (in OpenBSD 4.9, NetBSD 5.1 for
126      dnl example): getcwd (NULL, 0) fails rather than returning a string
127      dnl longer than PATH_MAX.  This may be POSIX compliant (in some
128      dnl interpretations of POSIX).  But gnulib's getcwd module wants to
129      dnl provide a non-NULL value in this case.
130      ret=$?
131      if test $ret -ge 128 || test $ret = 4; then
132        gl_cv_func_getcwd_abort_bug=yes
133      else
134        gl_cv_func_getcwd_abort_bug=no
135      fi],
136     [gl_cv_func_getcwd_abort_bug=yes])
137   ])
138   AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])
139 ])