On 2024-11-20 11:37, Bruno Haible wrote:
The comment no longer matches what the code does.

Thanks, good catch, as this also points out an infelicity in the code. I installed the attached to fix both comment and code.
From 3c8989db4436c9e8520807da1f9508b9ce426d62 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 20 Nov 2024 17:51:05 -0800
Subject: [PATCH] =?UTF-8?q?openat:=20don=E2=80=99t=20close=20(-1)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lib/openat.c (openat_permissive): Don’t close (-1), as some
debugging systems dislike that.  Clarify errno handling a bit.
---
 ChangeLog    |  4 ++++
 lib/openat.c | 12 +++++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e03ab61ceb..9d412f9cbc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-11-20  Paul Eggert  <egg...@cs.ucla.edu>
 
+	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;
         }
     }
 
-- 
2.43.0

Reply via email to