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