From: Paul Eggert Date: Thu, 21 Nov 2024 01:51:05 +0000 (-0800) Subject: openat: don’t close (-1) X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=3c8989db4436c9e8520807da1f9508b9ce426d62;p=gnulib.git openat: don’t close (-1) * lib/openat.c (openat_permissive): Don’t close (-1), as some debugging systems dislike that. Clarify errno handling a bit. --- diff --git a/ChangeLog b/ChangeLog index e03ab61ceb..9d412f9cbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2024-11-20 Paul Eggert + openat: don’t close (-1) + * lib/openat.c (openat_permissive): Don’t close (-1), as some + debugging systems dislike that. Clarify errno handling a bit. + tests: omit MEMCHR etc as no longer needed * tests/test-memchr.c (MEMCHR): * tests/test-memchr2.c (MEMCHR2): diff --git a/lib/openat.c b/lib/openat.c index 0e8e819408..609b03d627 100644 --- a/lib/openat.c +++ b/lib/openat.c @@ -277,14 +277,16 @@ openat_permissive (int fd, char const *file, int flags, mode_t mode, saved_errno = errno; if (!save_failed && restore_cwd (&saved_cwd) < 0) { + int restore_cwd_errno = errno; if (! cwd_errno) { - /* Don't write a message to just-created fd 2. */ - saved_errno = errno; - close (err); - openat_restore_fail (saved_errno); + /* Do not leak ERR. This also stops openat_restore_fail + from messing up if ERR happens to equal STDERR_FILENO. */ + if (0 <= err) + close (err); + openat_restore_fail (restore_cwd_errno); } - *cwd_errno = errno; + *cwd_errno = restore_cwd_errno; } }