From 8e968d52f7af1df86a44dd7c2bfe8d7d7088edc9 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 18 Nov 2024 07:54:54 -0800 Subject: [PATCH] =?utf8?q?openat:=20don=E2=80=99t=20lose=20track=20of=20sa?= =?utf8?q?ve=5Fcwd=20errno?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 + + 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 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.39.5