b73f47430b63e1ba07f0972348b78e7cb003ee1e
[deliverable/binutils-gdb.git] / gnulib / import / 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-2016 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 <errno.h>
31 #include <stdlib.h>
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #else /* on Windows with MSVC */
35 # include <direct.h>
36 #endif
37 #include <string.h>
38 #include <sys/stat.h>
39
40 ]gl_PATHMAX_SNIPPET[
41
42 /* Don't get link errors because mkdir is redefined to rpl_mkdir. */
43 #undef mkdir
44
45 #ifndef S_IRWXU
46 # define S_IRWXU 0700
47 #endif
48
49 /* FIXME: skip the run-test altogether on systems without getpagesize. */
50 #if ! HAVE_GETPAGESIZE
51 # define getpagesize() 0
52 #endif
53
54 /* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
55 the 16kB pagesize on ia64 linux. Those conditions make the code below
56 trigger a bug in glibc's getcwd implementation before 2.4.90-10. */
57 #define TARGET_LEN (5 * 1024)
58
59 int
60 main ()
61 {
62 char *cwd;
63 size_t initial_cwd_len;
64 int fail = 0;
65
66 /* The bug is triggered when PATH_MAX < getpagesize (), so skip
67 this relatively expensive and invasive test if that's not true. */
68 #ifdef PATH_MAX
69 int bug_possible = PATH_MAX < getpagesize ();
70 #else
71 int bug_possible = 0;
72 #endif
73 if (! bug_possible)
74 return 0;
75
76 cwd = getcwd (NULL, 0);
77 if (cwd == NULL)
78 return 2;
79
80 initial_cwd_len = strlen (cwd);
81 free (cwd);
82
83 if (1)
84 {
85 static char const dir_name[] = "confdir-14B---";
86 size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
87 / sizeof dir_name);
88 size_t d;
89 for (d = 0; d < desired_depth; d++)
90 {
91 if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
92 {
93 if (! (errno == ERANGE || errno == ENAMETOOLONG
94 || errno == ENOENT))
95 fail = 3; /* Unable to construct deep hierarchy. */
96 break;
97 }
98 }
99
100 /* If libc has the bug in question, this invocation of getcwd
101 results in a failed assertion. */
102 cwd = getcwd (NULL, 0);
103 if (cwd == NULL)
104 fail = 4; /* getcwd didn't assert, but it failed for a long name
105 where the answer could have been learned. */
106 free (cwd);
107
108 /* Call rmdir first, in case the above chdir failed. */
109 rmdir (dir_name);
110 while (0 < d--)
111 {
112 if (chdir ("..") < 0)
113 {
114 fail = 5;
115 break;
116 }
117 rmdir (dir_name);
118 }
119 }
120
121 return fail;
122 }
123 ]])],
124 [gl_cv_func_getcwd_abort_bug=no],
125 [dnl An abort will provoke an exit code of something like 134 (128 + 6).
126 dnl An exit code of 4 can also occur (in OpenBSD 4.9, NetBSD 5.1 for
127 dnl example): getcwd (NULL, 0) fails rather than returning a string
128 dnl longer than PATH_MAX. This may be POSIX compliant (in some
129 dnl interpretations of POSIX). But gnulib's getcwd module wants to
130 dnl provide a non-NULL value in this case.
131 ret=$?
132 if test $ret -ge 128 || test $ret = 4; then
133 gl_cv_func_getcwd_abort_bug=yes
134 else
135 gl_cv_func_getcwd_abort_bug=no
136 fi],
137 [gl_cv_func_getcwd_abort_bug=yes])
138 ])
139 AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])
140 ])
This page took 0.050529 seconds and 3 git commands to generate.