# HG changeset patch # User Eric Blake # Date 1257946442 25200 # Node ID 0583a6917a640a264dbcd4288184b0e5f1e4a17e # Parent 2518d73aaac78289215027f361f55ec6252bbef3 readlink: detect FreeBSD bug readlink("link-to-symlink/",buf,len) mistakenly read the contents of "symlink", rather than failing. * m4/readlink.m4 (gl_FUNC_READLINK): Also detect FreeBSD bug with slash on symlink. * doc/posix-functions/readlink.texi (readlink): Document the bug. * tests/test-readlink.h (test_readlink): Enhance test. Signed-off-by: Eric Blake diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2009-11-11 Eric Blake + readlink: detect FreeBSD bug + * m4/readlink.m4 (gl_FUNC_READLINK): Also detect FreeBSD bug with + slash on symlink. + * doc/posix-functions/readlink.texi (readlink): Document the bug. + * tests/test-readlink.h (test_readlink): Enhance test. + symlink: detect FreeBSD bug * m4/symlink.m4 (gl_FUNC_SYMLINK): Also detect FreeBSD bug with slash on symlink. diff --git a/doc/posix-functions/readlink.texi b/doc/posix-functions/readlink.texi --- a/doc/posix-functions/readlink.texi +++ b/doc/posix-functions/readlink.texi @@ -10,7 +10,7 @@ @itemize @item Some platforms mistakenly succeed on @code{readlink("link/",buf,len)}: -Solaris 9. +FreeBSD 7.2, Solaris 9. @item On some platforms, @code{readlink} returns @code{int} instead of @code{ssize_t}: diff --git a/m4/readlink.m4 b/m4/readlink.m4 --- a/m4/readlink.m4 +++ b/m4/readlink.m4 @@ -1,4 +1,4 @@ -# readlink.m4 serial 7 +# readlink.m4 serial 8 dnl Copyright (C) 2003, 2007, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -21,15 +21,18 @@ /* Cause compilation failure if original declaration has wrong type. */ ssize_t readlink (const char *, char *, size_t);]])], [gl_cv_decl_readlink_works=yes], [gl_cv_decl_readlink_works=no])]) + dnl Solaris 9 ignores trailing slash. + dnl FreeBSD 7.2 dereferences only one level of links with trailing slash. AC_CACHE_CHECK([whether readlink handles trailing slash correctly], [gl_cv_func_readlink_works], [# We have readlink, so assume ln -s works. ln -s conftest.no-such conftest.link + ln -s conftest.link conftest.lnk2 AC_RUN_IFELSE( [AC_LANG_PROGRAM( [[#include ]], [[char buf[20]; - return readlink ("conftest.link/", buf, sizeof buf) != -1;]])], + return readlink ("conftest.lnk2/", buf, sizeof buf) != -1;]])], [gl_cv_func_readlink_works=yes], [gl_cv_func_readlink_works=no], [gl_cv_func_readlink_works="guessing no"]) rm -f conftest.link]) diff --git a/tests/test-readlink.h b/tests/test-readlink.h --- a/tests/test-readlink.h +++ b/tests/test-readlink.h @@ -52,11 +52,11 @@ errno = 0; ASSERT (func (BASE "file/", buf, sizeof buf) == -1); ASSERT (errno == ENOTDIR); - ASSERT (unlink (BASE "file") == 0); /* Now test actual symlinks. */ if (symlink (BASE "dir", BASE "link")) { + ASSERT (unlink (BASE "file") == 0); if (print) fputs ("skipping test: symlinks not supported on this file system\n", stderr); @@ -66,6 +66,17 @@ errno = 0; ASSERT (func (BASE "link/", buf, sizeof buf) == -1); ASSERT (errno == EINVAL); + ASSERT (symlink (BASE "link", BASE "link2") == 0); + errno = 0; + ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); + ASSERT (errno == EINVAL); + ASSERT (unlink (BASE "link2") == 0); + ASSERT (symlink (BASE "file", BASE "link2") == 0); + errno = 0; + ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); + ASSERT (errno == ENOTDIR); + ASSERT (unlink (BASE "file") == 0); + ASSERT (unlink (BASE "link2") == 0); { /* Up till now, no readlink has been successful, so buf should be unchanged. */