>From 6ee6c7189f4bafbb603ef043fdf44b84c3f7532a Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 17 Jan 2021 22:07:14 +0100 Subject: [PATCH 2/3] canonicalize-lgpl: Work around // handling in realpath() of musl 1.2.2. Reported by Natanael Copa in . * m4/canonicalize.m4 (gl_FUNC_REALPATH_WORKS): Add a test whether // is the same as /, on Linux only. * lib/canonicalize-lgpl.c: Correct indentation of preprocessor directives. * doc/posix-functions/realpath.texi: Mention the musl 1.2.2 bug. --- ChangeLog | 11 +++++++++++ doc/posix-functions/realpath.texi | 4 ++-- lib/canonicalize-lgpl.c | 19 ++++++++++--------- m4/canonicalize.m4 | 35 ++++++++++++++++++++++++++++------- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index dad3119..3570066 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2021-01-17 Bruno Haible + canonicalize-lgpl: Work around // handling in realpath() of musl 1.2.2. + Reported by Natanael Copa in + . + * m4/canonicalize.m4 (gl_FUNC_REALPATH_WORKS): Add a test whether // is + the same as /, on Linux only. + * lib/canonicalize-lgpl.c: Correct indentation of preprocessor + directives. + * doc/posix-functions/realpath.texi: Mention the musl 1.2.2 bug. + +2021-01-17 Bruno Haible + canonicalize[-lgpl] tests: Add more tests. * tests/test-canonicalize.c (main): Add detailed tests for // handling. * tests/test-canonicalize-lgpl.c (main): Likewise. diff --git a/doc/posix-functions/realpath.texi b/doc/posix-functions/realpath.texi index 02837e7..a5b7c1d 100644 --- a/doc/posix-functions/realpath.texi +++ b/doc/posix-functions/realpath.texi @@ -22,14 +22,14 @@ Solaris 10. @item This function fails to detect trailing slashes on non-directories on some platforms: -glibc 2.3.5, Mac OS X 10.13. +glibc 2.3.5, Mac OS X 10.13, OpenBSD 6.0. @item This function fails to recognize non-directories followed @samp{..} on some platforms: cygwin. @item This function misbehaves on consecutive slashes on some platforms: -AIX 7. +musl libc 1.2.2, AIX 7. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/canonicalize-lgpl.c b/lib/canonicalize-lgpl.c index 698f9ed..bf63355 100644 --- a/lib/canonicalize-lgpl.c +++ b/lib/canonicalize-lgpl.c @@ -141,11 +141,11 @@ suffix_requires_dir_check (char const *end) macOS 10.13 , and should also work on platforms like AIX 7.2 that need at least "/.". */ -#if defined _LIBC || defined LSTAT_FOLLOWS_SLASHED_SYMLINK +# if defined _LIBC || defined LSTAT_FOLLOWS_SLASHED_SYMLINK static char const dir_suffix[] = "/"; -#else +# else static char const dir_suffix[] = "/./"; -#endif +# endif /* Return true if DIR is a searchable dir, false (setting errno) otherwise. DIREND points to the NUL byte at the end of the DIR string. @@ -187,13 +187,13 @@ get_path_max (void) to pacify GCC is known; even an explicit #pragma does not pacify GCC. When the GCC bug is fixed this workaround should be limited to the broken GCC versions. */ -#if __GNUC_PREREQ (10, 1) -# if defined GCC_LINT || defined lint +# if __GNUC_PREREQ (10, 1) +# if defined GCC_LINT || defined lint __attribute__ ((__noinline__)) -# elif __OPTIMIZE__ && !__NO_INLINE__ -# define GCC_BOGUS_WRETURN_LOCAL_ADDR +# elif __OPTIMIZE__ && !__NO_INLINE__ +# define GCC_BOGUS_WRETURN_LOCAL_ADDR +# endif # endif -#endif static char * realpath_stk (const char *name, char *resolved, struct scratch_buffer *rname_buf) @@ -439,7 +439,8 @@ __realpath (const char *name, char *resolved) } libc_hidden_def (__realpath) versioned_symbol (libc, __realpath, realpath, GLIBC_2_3); -#endif /* !FUNC_REALPATH_WORKS || defined _LIBC */ + +#endif /* defined _LIBC || !FUNC_REALPATH_WORKS */ #if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_3) diff --git a/m4/canonicalize.m4 b/m4/canonicalize.m4 index 475fa15..6821c70 100644 --- a/m4/canonicalize.m4 +++ b/m4/canonicalize.m4 @@ -1,4 +1,4 @@ -# canonicalize.m4 serial 35 +# canonicalize.m4 serial 36 dnl Copyright (C) 2003-2007, 2009-2021 Free Software Foundation, Inc. @@ -91,6 +91,7 @@ AC_DEFUN([gl_FUNC_REALPATH_WORKS], #include ]], [[ int result = 0; + /* This test fails on Solaris 10. */ { char *name = realpath ("conftest.a", NULL); if (!(name && *name == '/')) @@ -103,12 +104,14 @@ AC_DEFUN([gl_FUNC_REALPATH_WORKS], result |= 2; free (name); } + /* This test fails on Mac OS X 10.13, OpenBSD 6.0. */ { char *name = realpath ("conftest.a/", NULL); if (name != NULL) result |= 4; free (name); } + /* This test fails on AIX 7, Solaris 10. */ { char *name1 = realpath (".", NULL); char *name2 = realpath ("conftest.d//./..", NULL); @@ -117,16 +120,31 @@ AC_DEFUN([gl_FUNC_REALPATH_WORKS], free (name1); free (name2); } + #ifdef __linux__ + /* On Linux, // is the same as /. See also double-slash-root.m4. + realpath() should respect this. + This test fails on musl libc 1.2.2. */ + { + char *name = realpath ("//", NULL); + if (! name || strcmp (name, "/")) + result |= 16; + free (name); + } + #endif return result; ]]) ], [gl_cv_func_realpath_works=yes], - [gl_cv_func_realpath_works=no], + [case $? in + 16) gl_cv_func_realpath_works=nearly ;; + *) gl_cv_func_realpath_works=no ;; + esac + ], [case "$host_os" in # Guess yes on glibc systems. *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;; - # Guess yes on musl systems. - *-musl*) gl_cv_func_realpath_works="guessing yes" ;; + # Guess 'nearly' on musl systems. + *-musl*) gl_cv_func_realpath_works="guessing nearly" ;; # Guess no on native Windows. mingw*) gl_cv_func_realpath_works="guessing no" ;; # If we don't know, obey --enable-cross-guesses. @@ -137,9 +155,12 @@ AC_DEFUN([gl_FUNC_REALPATH_WORKS], ]) case "$gl_cv_func_realpath_works" in *yes) - AC_DEFINE([FUNC_REALPATH_WORKS], [1], [Define to 1 if realpath() - can malloc memory, always gives an absolute path, and handles - trailing slash correctly.]) + AC_DEFINE([FUNC_REALPATH_WORKS], [1], + [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles leading slashes and a trailing slash correctly.]) + ;; + *nearly) + AC_DEFINE([FUNC_REALPATH_NEARLY_WORKS], [1], + [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles a trailing slash correctly.]) ;; esac ]) -- 2.7.4