* lib/openat.c (openat_permissive): When ‘close’ is called
after save_cwd failed, don’t lose track of save_cwd’s errno.
This fixes a recently-introduced unlikely bug.
---
 ChangeLog    | 7 +++++++
 lib/openat.c | 8 ++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 79e240c1fc..13f68f57ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-11-20  Paul Eggert  <egg...@cs.ucla.edu>
+
+       openat: don’t lose track of save_cwd errno
+       * lib/openat.c (openat_permissive): When ‘close’ is called
+       after save_cwd failed, don’t lose track of save_cwd’s errno.
+       This fixes a recently-introduced unlikely bug.
+
 2024-11-20  Bruno Haible  <br...@clisp.org>
 
        c-stack: Avoid a busy-looping test on macOS.
diff --git a/lib/openat.c b/lib/openat.c
index 698874427b..c5d5d7d827 100644
--- a/lib/openat.c
+++ b/lib/openat.c
@@ -244,7 +244,7 @@ openat_permissive (int fd, char const *file, int flags, 
mode_t mode,
       }
   }
 
-  bool save_failed = save_cwd (&saved_cwd) < 0;
+  int save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0;
 
   /* If save_cwd allocated a descriptor DFD other than FD, do another
      save_cwd and then close DFD, so that the later open (if successful)
@@ -252,7 +252,7 @@ openat_permissive (int fd, char const *file, int flags, 
mode_t mode,
   int dfd = saved_cwd.desc;
   if (0 <= dfd && dfd != fd)
     {
-      save_failed = save_cwd (&saved_cwd) < 0;
+      save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0;
       close (dfd);
       dfd = saved_cwd.desc;
     }
@@ -269,8 +269,8 @@ openat_permissive (int fd, char const *file, int flags, 
mode_t mode,
   if (save_failed)
     {
       if (! cwd_errno)
-        openat_save_fail (errno);
-      *cwd_errno = errno;
+        openat_save_fail (save_failed);
+      *cwd_errno = save_failed;
     }
 
   err = fchdir (fd);
-- 
2.47.0


Reply via email to