Let's see whether some kernel confuses the notion of "file descriptor" with the notion of "file descriptor or AT_FDCWD". A little bit of "let's try to provoke undefined behaviour internally" mindset... 😉
2024-09-03 Bruno Haible <br...@clisp.org> tests: Strenghten tests with invalid file descriptor. * tests/test-close.c: Include <fcntl.h>. (main): Check that AT_FDCWD is recognized as an invalid file descriptor. * tests/test-dup.c: Include <fcntl.h>. (main): Check that AT_FDCWD is recognized as an invalid file descriptor. * tests/test-unlockpt.c: Include <fcntl.h>. (main): Check that AT_FDCWD is recognized as an invalid file descriptor. * tests/test-fchdir.c (main): Likewise. * tests/test-fdatasync.c (main): Likewise. * tests/test-fdopendir.c (main): Likewise. * tests/test-fsync.c (main): Likewise. * tests/test-isatty.c (main): Likewise. diff --git a/tests/test-close.c b/tests/test-close.c index b796e0d200..ca2c21bfe2 100644 --- a/tests/test-close.c +++ b/tests/test-close.c @@ -22,6 +22,7 @@ SIGNATURE_CHECK (close, int, (int)); #include <errno.h> +#include <fcntl.h> #include "macros.h" @@ -40,6 +41,13 @@ main (void) ASSERT (close (99) == -1); ASSERT (errno == EBADF); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (close (AT_FDCWD) == -1); + ASSERT (errno == EBADF); + } +#endif return test_exit_status; } diff --git a/tests/test-dup.c b/tests/test-dup.c index bf3f519bb8..884fbc605c 100644 --- a/tests/test-dup.c +++ b/tests/test-dup.c @@ -22,6 +22,7 @@ SIGNATURE_CHECK (dup, int, (int)); #include <errno.h> +#include <fcntl.h> #include "macros.h" @@ -40,6 +41,13 @@ main (void) ASSERT (dup (99) == -1); ASSERT (errno == EBADF); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (dup (AT_FDCWD) == -1); + ASSERT (errno == EBADF); + } +#endif return test_exit_status; } diff --git a/tests/test-fchdir.c b/tests/test-fchdir.c index 692ab5194c..0eba209c47 100644 --- a/tests/test-fchdir.c +++ b/tests/test-fchdir.c @@ -56,6 +56,13 @@ main (void) ASSERT (fchdir (99) == -1); ASSERT (errno == EBADF); } +#ifdef FD_ATCWD + { + errno = 0; + ASSERT (fchdir (FD_ATCWD) == -1); + ASSERT (errno == EBADF); + } +#endif /* Check for other failure cases. */ { diff --git a/tests/test-fdatasync.c b/tests/test-fdatasync.c index 3d55a7105b..691a198a99 100644 --- a/tests/test-fdatasync.c +++ b/tests/test-fdatasync.c @@ -56,6 +56,13 @@ main (void) ASSERT (fdatasync (99) == -1); ASSERT (errno == EBADF); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (fdatasync (AT_FDCWD) == -1); + ASSERT (errno == EBADF); + } +#endif fd = open (file, O_WRONLY|O_CREAT|O_TRUNC, 0644); ASSERT (0 <= fd); diff --git a/tests/test-fdopendir.c b/tests/test-fdopendir.c index 5dd8a4a967..3430d03bad 100644 --- a/tests/test-fdopendir.c +++ b/tests/test-fdopendir.c @@ -56,6 +56,13 @@ main () ASSERT (fdopendir (99) == NULL); ASSERT (errno == EBADF); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (fdopendir (AT_FDCWD) == NULL); + ASSERT (errno == EBADF); + } +#endif /* This should work. */ fd = open (".", O_RDONLY); diff --git a/tests/test-fsync.c b/tests/test-fsync.c index c28a5cc6ab..7e680c274a 100644 --- a/tests/test-fsync.c +++ b/tests/test-fsync.c @@ -56,6 +56,13 @@ main (void) ASSERT (fsync (99) == -1); ASSERT (errno == EBADF); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (fsync (AT_FDCWD) == -1); + ASSERT (errno == EBADF); + } +#endif fd = open (file, O_WRONLY|O_CREAT|O_TRUNC, 0644); ASSERT (0 <= fd); diff --git a/tests/test-isatty.c b/tests/test-isatty.c index 1c7d6d8cd9..d844c62615 100644 --- a/tests/test-isatty.c +++ b/tests/test-isatty.c @@ -56,6 +56,15 @@ main (void) || errno == 0 /* seen on IRIX 6.5, Solaris 10 */ ); } +#ifdef AT_FDCWD + { + errno = 0; + ASSERT (isatty (AT_FDCWD) == 0); + ASSERT (errno == EBADF + || errno == 0 /* seen on IRIX 6.5, Solaris 10 */ + ); + } +#endif /* Test behaviour for regular files. */ { diff --git a/tests/test-unlockpt.c b/tests/test-unlockpt.c index e69f72db97..8fbd36cf69 100644 --- a/tests/test-unlockpt.c +++ b/tests/test-unlockpt.c @@ -22,6 +22,7 @@ SIGNATURE_CHECK (unlockpt, int, (int)); #include <errno.h> +#include <fcntl.h> #include <unistd.h> #include "macros.h" @@ -46,6 +47,15 @@ main (void) || errno == EINVAL /* seen on FreeBSD 6.4 */ ); } +# ifdef AT_FDCWD + { + errno = 0; + ASSERT (unlockpt (AT_FDCWD) == -1); + ASSERT (errno == EBADF + || errno == EINVAL /* seen on FreeBSD 6.4 */ + ); + } +# endif #endif return test_exit_status;