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.
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.
13 # gl_FUNC_GETCWD_ABORT_BUG([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
14 AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
16 AC_CHECK_DECLS_ONCE([getcwd])
17 AC_CHECK_HEADERS_ONCE([unistd.h])
18 AC_REQUIRE([gl_PATHMAX_SNIPPET_PREREQ])
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.])
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.
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.
40 #else /* on Windows with MSVC */
48 /* Don't get link errors because mkdir is redefined to rpl_mkdir. */
55 /* FIXME: skip the run-test altogether on systems without getpagesize. */
56 #if ! HAVE_GETPAGESIZE
57 # define getpagesize() 0
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)
69 size_t initial_cwd_len;
72 /* The bug is triggered when PATH_MAX < getpagesize (), so skip
73 this relatively expensive and invasive test if that's not true. */
75 int bug_possible = PATH_MAX < getpagesize ();
82 cwd = getcwd (NULL, 0);
86 initial_cwd_len = strlen (cwd);
91 static char const dir_name[] = "confdir-14B---";
92 size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
95 for (d = 0; d < desired_depth; d++)
97 if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
99 if (! (errno == ERANGE || errno == ENAMETOOLONG
101 fail = 3; /* Unable to construct deep hierarchy. */
106 /* If libc has the bug in question, this invocation of getcwd
107 results in a failed assertion. */
108 cwd = getcwd (NULL, 0);
110 fail = 4; /* getcwd didn't assert, but it failed for a long name
111 where the answer could have been learned. */
114 /* Call rmdir first, in case the above chdir failed. */
118 if (chdir ("..") < 0)
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.
138 if test $ret -ge 128 || test $ret = 4; then
139 gl_cv_func_getcwd_abort_bug=yes
141 gl_cv_func_getcwd_abort_bug=no
143 [gl_cv_func_getcwd_abort_bug=yes])
145 AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])