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-2021 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-BUGGY[, ACTION-IF-WORKS]])
14 AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
16 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 AC_CHECK_DECLS_ONCE([getcwd])
18 AC_CHECK_HEADERS_ONCE([unistd.h])
19 AC_REQUIRE([gl_PATHMAX_SNIPPET_PREREQ])
21 gl_CHECK_FUNC_GETPAGESIZE
22 if test $gl_cv_func_getpagesize = yes; then
23 AC_DEFINE_UNQUOTED([HAVE_GETPAGESIZE], [1],
24 [Define to 1 if the system has the 'getpagesize' function.])
27 AC_CACHE_CHECK([whether getcwd succeeds when 4k < cwd_length < 16k],
28 [gl_cv_func_getcwd_succeeds_beyond_4k],
29 [# Remove any remnants of a previous test.
31 # Arrange for deletion of the temporary directory this test creates.
32 ac_clean_files="$ac_clean_files confdir-14B---"
33 dnl Please keep this in sync with tests/test-getcwd.c.
41 #else /* on Windows with MSVC */
54 /* FIXME: skip the run-test altogether on systems without getpagesize. */
55 #if ! HAVE_GETPAGESIZE
56 # define getpagesize() 0
59 /* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
60 the 16kB pagesize on ia64 linux. Those conditions make the code below
61 trigger a bug in glibc's getcwd implementation before 2.4.90-10. */
62 #define TARGET_LEN (5 * 1024)
68 size_t initial_cwd_len;
71 /* The bug is triggered when PATH_MAX < getpagesize (), so skip
72 this relatively expensive and invasive test if that's not true. */
74 int bug_possible = PATH_MAX < getpagesize ();
81 cwd = getcwd (NULL, 0);
85 initial_cwd_len = strlen (cwd);
90 static char const dir_name[] = "confdir-14B---";
91 size_t desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
94 for (d = 0; d < desired_depth; d++)
96 if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
98 if (! (errno == ERANGE || errno == ENAMETOOLONG
100 fail = 3; /* Unable to construct deep hierarchy. */
105 /* If libc has the bug in question, this invocation of getcwd
106 results in a failed assertion. */
107 cwd = getcwd (NULL, 0);
109 fail = 4; /* getcwd didn't assert, but it failed for a long name
110 where the answer could have been learned. */
113 /* Call rmdir first, in case the above chdir failed. */
117 if (chdir ("..") < 0)
129 [gl_cv_func_getcwd_succeeds_beyond_4k=yes],
130 [dnl An abort will provoke an exit code of something like 134 (128 + 6).
131 dnl An exit code of 4 can also occur (for example in
132 dnl musl libc 1.2.2/powerpc64le, NetBSD 9.0, OpenBSD 6.7:
133 dnl getcwd (NULL, 0) fails rather than returning a string longer than
134 dnl PATH_MAX. This may be POSIX compliant (in some interpretations of
135 dnl POSIX). But gnulib's getcwd module wants to provide a non-NULL
136 dnl value in this case.
138 if test $ret -ge 128 || test $ret = 4; then
139 gl_cv_func_getcwd_succeeds_beyond_4k=no
141 gl_cv_func_getcwd_succeeds_beyond_4k=yes
145 # Guess no otherwise, even on glibc systems and musl systems.
146 *) gl_cv_func_getcwd_succeeds_beyond_4k="guessing no"
150 case "$gl_cv_func_getcwd_succeeds_beyond_4k" in