* tests/test-openat.c (main): Work even if AT_FDCWD == -1.
---
 ChangeLog                    | 5 +++++
 doc/posix-headers/fcntl.texi | 7 +++++++
 tests/test-openat.c          | 2 +-
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 46e8238da1..0e3d2c7f1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2025-09-01  Paul Eggert  <[email protected]>
+
+       openat2-tests: fix AT_FDCWD portability
+       * tests/test-openat.c (main): Work even if AT_FDCWD == -1.
+
 2025-08-31  Collin Funk  <[email protected]>
 
        u64: Allow the header to be included twice.
diff --git a/doc/posix-headers/fcntl.texi b/doc/posix-headers/fcntl.texi
index 75d9128cd8..354bae0193 100644
--- a/doc/posix-headers/fcntl.texi
+++ b/doc/posix-headers/fcntl.texi
@@ -149,4 +149,11 @@ fcntl.h
 @samp{POSIX_FADV_NORMAL}, @samp{POSIX_FADV_RANDOM},
 @samp{POSIX_FADV_SEQUENTIAL}, and @samp{POSIX_FADV_WILLNEED} are not
 defined on some platforms.
+
+@item
+POSIX allows @samp{AT_FDCWD} to be @minus{}1.
+Even though @samp{AT_FDCWD} is less than @minus{}1 on all
+known practical platforms,
+it is better to not assume that @minus{}1 is an invalid argument
+to functions like @samp{openat}.
 @end itemize
diff --git a/tests/test-openat.c b/tests/test-openat.c
index 1405f4f7a5..5b904f97fd 100644
--- a/tests/test-openat.c
+++ b/tests/test-openat.c
@@ -69,7 +69,7 @@ main ()
   /* Test behaviour for invalid file descriptors.  */
   {
     errno = 0;
-    ASSERT (openat (-1, "foo", O_RDONLY) == -1);
+    ASSERT (openat (AT_FDCWD == -1 ? -2 : -1, "foo", O_RDONLY) == -1);
     ASSERT (errno == EBADF);
   }
   {
-- 
2.51.0


Reply via email to